How to combine multiple strings into unicode?

SL2 20 Reputation points
2023-01-15T15:58:28.88+00:00

Hi!
Question: How do I combine three strings into one AND use it as Unicode? The need for \ messes everything up.

string prefix = @"\U0001F0";
string hexSuit = "A";
string hexValue = "B";     
        

I want to put together these three strings into "\U0001F0AB" that's actually usable as Unicode in C#.
Is there a way to do this?

Background: I'm making a card game in Forms. It will have two enums, Suit and Value, for the cards, which I convert to hex in order to get the corresponding character in Unicode (page two: [https://www.unicode.org/charts/PDF/U1F0A0.pdf although I will use 52 cards, not 56)

In the example above, (int)suit equals 10, which gets converted to A in hex, and (int)value equals 11, which is B in hex. This gives me the Jack of spades character. Everything works as expected when hardcoding "\U0001F0AB" into Form1.
I know I could solve this with an x52 Switch, or just use images, but I haven't given up this just yet.

    public enum Suit
    {
        Spades = 10,
        Hearts,
        Diamonds,
        Clubs
    }


    public enum Value
    {
        Ace = 1,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen,
        King
    }
Developer technologies Windows Forms
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-01-15T16:24:08.1833333+00:00

    Try one of possibilities:

    public enum Suit
    {
        Spades = 10,
        Hearts,
        Diamonds,
        Clubs
    }
    
    public enum Value
    {
        Ace = 1,
        Two,
        Three,
        Four,
        Five,
        Six,
        Seven,
        Eight,
        Nine,
        Ten,
        Jack,
        Queen = Jack + 2, // MODIFIED
        King
    }
    
    
    // example
    
    Suit suit = Suit.Spades;
    Value value = Value.Ace;
    
    Int32 u = 0x00010000 | ((((0xF0 << 4) | (int)suit) << 4) | (int)value);
    
    string result = char.ConvertFromUtf32( u );
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.