0

I have a large 2D jagged edge array

string[][] MagicString = new string[417][];
MagicString[0] = new string[] { "Winner", "\u0041", "\u0042", "\u0043"};
//and so on only show first item in 2D here.

The strings are \u to make them Unicode value. I have them as Unicode as we move through the Array we can get Characters from outside the ASCII range. I also uses these values to display the Magic string in a different tab. In the case above we see A in a cell then B in next cell and then C etc. All this Unicode strings works fine.

string[] InnerCharArray = MagicString[cmbobx_sounds.SelectedIndex];
..
..
dataGridView1[grid_y, grid_x].Value = InnerCharArray[char_num];

Now the bit I cannot get to work. In a different tab the user inputs a character and we check through the arrays to find it then print out the first item which may say "Winner"

In the string below called Unicodevalue for input of A I cannot get it to be \u0041. It will always return double slash but I only want one slash \ \u0041. How can I read the text from the RichTextBox and turn it into a string \u0041, same as I have in the Array above?

for (int i = 0; i < rchtxbx_input.Text.Length; i++)
        {                
            string UnicodeValue =  @"\u" + String.Format("{0:x4}", Convert.ToInt32(rchtxbx_input.Text[i]));
 //tried "\\u" @"\u" just u removed u altogether

            for (int j = 0; j < MagicString.Length; j++)
            {
                string[] InnerCharArray = MagicString[j];
                bool has = InnerCharArray.Contains(UnicodeValue);
/never goes true even when I type in A which is 0041 as in Array

                if (has) rchtxbx_output.AppendText(InnerCharArray[0]);
            }
        }
user3884423
  • 556
  • 1
  • 5
  • 20
  • Apparently related to [How do convert unicode escape sequences to unicode characters in a .NET string](http://stackoverflow.com/questions/183907/). – Jeppe Stig Nielsen Jan 18 '17 at 16:36
  • And also http://stackoverflow.com/questions/7826162/how-do-i-get-the-decimal-value-of-a-unicode-character-in-c. You should consider using an array of `char`s rather than strings... – Heretic Monkey Jan 18 '17 at 16:39
  • All `char` are UTF-16 code units, one or two of which encode a Unicode codepoint. ASCII doesn't have anything to do with it. Strings are counted sequences of `char`. So, "\u0041" is a string with one code unit, which happens to be the single code unit needed for the codepoint A. (In other words, "\u0041"=="A".). – Tom Blodget Jan 18 '17 at 18:04

2 Answers2

1

The thing is, that you do not store "\u0041" in your string array MagicString. You put "\u0041" in there, which is a correct unicode syntax, so c# converts it to "A" (or whatever character 41 is) internally.

Your mistake is in the declaration of the MagicString. There you need to put @"\u0041". If that doesn't work, cheat it by putting @"\" + "u0041" to have C# store it as string.

Grisgram
  • 3,105
  • 3
  • 25
  • 42
0

Thanks for help. Grisgram gave me the clue I had missed. In the array "\u0041" is a character and not a string. Once I see that I worked out how do sort it out. I have to leave them as Unicode Char as I draw them to the gridview.

So after a few changes I now get the Unicode chars into ints and then use them

int InPutChar = Convert.ToInt32(rchtxbx_input.Text[i]);

char c = '\u3400';
int LowestMagicChar = c;

The rest of the code snippet is below

for (int i = 0; i < rchtxbx_input.Text.Length; i++)
        {

            string answers = "";

            int InPutChar = Convert.ToInt32(rchtxbx_input.Text[i]);

            char c = '\u3400';
            // Implicit conversion: char is basically a 16-bit unsigned integer
            int LowestMagicChar = c;

            bool doCheck = true, multipleChar = false; //set true if statements will set false.

            if (InPutChar < LowestMagicChar) 
            {
                rchtxbx_output.AppendText(rchtxbx_input.Text[i].ToString());
                doCheck = false;
            }

            if (doCheck == true) //Could be MagicChar so more checks inside
            {
                for (int j = 0; j < MagicString.Length; j++)
                {

                    string[] InnerCharArray = MagicString[j];

                    for (int k = 1; k < InnerCharArray.Length; k++)
                    {
                        string unichar = InnerCharArray[k]; //Get next char to check


                        if (rchtxbx_input.Text[i].ToString() == unichar) //&& (doCheck))
                                                                         //check with the char in textbox
                        {
                            if (doCheck == true)
                            {
                                answers = InnerCharArray[0]; //first time in we have one char
                            }
                            else
                            {
                                answers += ";" + InnerCharArray[0]; //comes in here if multiple chars for same sound
                                multipleChar = true;
                            }
                            // rchtxbx_output.AppendText(InnerCharArray[0] + " ");
                            //If the char then put down the sound and space.
                            doCheck = false;

Now I have it working I just need to make it better and quicker. Thanks to all for the help, often we do not see the obvious till it is pointed out.

user3884423
  • 556
  • 1
  • 5
  • 20