Search Unity

String/Unicode encoding problems!

Discussion in 'Scripting' started by 128bit, Sep 21, 2015.

  1. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    Hi,
    im trying to make the unicode "\u2665" look like "♥" in an string.
    I cant just use String.Replace since all different unicodes should be converted.
    Using this, it works fawlessly:

    Code (CSharp):
    1. string unicodeString = "\u2665";
    2.         // Create two different encodings.
    3.         Encoding ascii = Encoding.UTF8;
    4.         Encoding unicode = Encoding.Unicode;
    5.         // Convert the string into a byte array.
    6.         byte[] unicodeBytes = unicode.GetBytes(unicodeString);
    7.         // Perform the conversion from one encoding to the other.
    8.         byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);
    9.  
    10.         // Convert the new byte[] into a char[] and then into a string.
    11.         char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
    12.         ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
    13.         string asciiString = new string(asciiChars);
    14.         Debug.Log (asciiString);
    Tho if im declaring "unicodeString" as an public string and just set the value in the editor instead of inside the script, it just outputs ""\u2665" instead of "♥". Someone may know how to solve the encoding problem?
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    you might be able to put the @ symbol at the start. Not sure it will fix your problem, but it might.

    ie. string unicodestr = @"\u2665";
     
    128bit likes this.
  3. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    my guess is that the built in editor for strings automatically encodes anything you type here, so \ is probably serialized as \\ or something, so that it always comes back as \. In order to get around that, you might be able to make a custom editor which watches for special characters, or else you could figure out how to type ♥ into the editor (hint: alt + 3)
     
    128bit likes this.
  4. 128bit

    128bit

    Joined:
    Oct 8, 2014
    Posts:
    117
    Thanks, tho doesnt fix it when using an public variable and setting value in editor.

    Thanks. The point is, the final need for it will be to download an text via Downloadstring (that contains codes like \u2665) and convert it so it does display unicode characters. So just knowing how to type an heart or other unicodes wont be enough. Only strings that got declared like:
    string unicodeString ="\u2665";
    in script are working. All other, in editor or even when just downloaded. arent.Thats kinda confusing me.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    yeah it was a longshot.
     
  6. eisenpony

    eisenpony

    Joined:
    May 8, 2015
    Posts:
    974
    Okay, let's clear this up. The \u escape sequence is recognized by the c# specifications as a Unicode character escape sequence. So, it makes sense that the strings written in code would be converted into a Unicode character -- it is done by the compiler as a "preprocess", before regular compilation begins.

    Once you are into the runtime, e.g., downloading a string, that conversion will not happen automatically. If you want to convert Unicode patterns into Unicode characters, you will need to write a parser that can "unescape" these \u patterns. Looks like there is already a parser which can do this and much more (maybe too much) in the System.Text.RegularExpressions.Regex class called Unescape: https://msdn.microsoft.com/en-us/li...ularexpressions.regex.unescape(v=vs.110).aspx
     
    JoeStrout likes this.
  7. vncnt_klm

    vncnt_klm

    Joined:
    Jul 2, 2015
    Posts:
    1
    This worked for me:

    // ADD EURO SIGN
    FieldData.text += System.Convert.ToChar(0x20AC);
     
    naviln likes this.
  8. naviln

    naviln

    Joined:
    Oct 18, 2016
    Posts:
    32
    This is perfect, exactly what I needed. Thankyou!