Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find a character matching my alt code

Tags:

c#

wpf

When I type Alt+1 I get back ☺ I would like some magic way in code to get the ☺ character in a string.

Something like this:

string smiley = AltCodes.GetAltCodeCharacter(1).ToString();

// Display the Smiley
richTextBoxHappy.Text = smiley;

Then in the rich text box there should be this:

like image 454
Shiasu-sama Avatar asked May 15 '18 11:05

Shiasu-sama


2 Answers

You can use Unicode Table (Decimal) to find out your code, then use it like this for example:

int i = 9786;
textBoxHappy.Text = ((char)i).ToString();

Result image:

enter image description here

Edit

Of course you can create your custom mapping, for example some Dictionary<string, int> (or any custom type you want), and add codes to it like { "smiley", 9786 }, then use it altCode["smiley"].

Update

Or use enum for that:

enum AltCodes
{
    Smiley = 9786,
    NextSmiley = 9787
}

and use it like:

textBoxHappy.Text = ((char)AltCodes.Smiley).ToString();

You can even create your custom decoder, then write your alt code (by holding alt and typing numbers) inside wpf TextBox, click "Decode" and get your code values to int[] for example:

private int[] DecodeCharCode(string str) =>
    DecodeCharCode(str.ToArray());


private int[] DecodeCharCode(char[] chars)
{
    int[] result = new int[chars.Length];
    for (int i = 0; i < chars.Length; i++)
    {
        result[i] = (int)chars[i];
    }
    return result;
}

and usage (click event or command):

int[] decodedCodes = DecodeCharCode(decodingText.Text);
like image 180
SᴇM Avatar answered Nov 01 '22 18:11

SᴇM


There is a finite number of these. You can just create a Dictionary<int, string> to store all these:

IReadOnlyDictionary<int, string> altCodes = new Dictionary<int, string>() {
    {1, "☺"},
    {2, "\U0000263B"},
    // and so on...
};

And access it like so:

altCodes[1]
like image 23
Sweeper Avatar answered Nov 01 '22 19:11

Sweeper