Best way to decode hex sequence of unicode characters to string

一个人想着一个人 提交于 2019-12-21 17:54:29

问题


I'm working with C# .Net

I would like to know how to convert a Unicode form string like "\u1D0EC" (note that it's above "\uFFFF") to it's symbol... "𝃬"

Thanks For Advance!!!


回答1:


That Unicode codepoint is encoded in UTF32. .NET and Windows encode Unicode in UTF16, you'll have to translate. UTF16 uses "surrogate pairs" to handle codepoints above 0xffff, a similar kind of approach as UTF8. The first code of the pair is 0xd800..dbff, the second code is 0xdc00..dfff. Try this sample code to see that at work:

using System;
using System.Text;

class Program {
  static void Main(string[] args) {
    uint utf32 = uint.Parse("1D0EC", System.Globalization.NumberStyles.HexNumber);
    string s = Encoding.UTF32.GetString(BitConverter.GetBytes(utf32));
    foreach (char c in s.ToCharArray()) {
      Console.WriteLine("{0:X}", (uint)c);
    }
    Console.ReadLine();
  }
}



回答2:


Convert each sequence with int.Parse(String, NumberStyles) and char.ConvertFromUtf32:

string s = @"\U1D0EC";
string converted = char.ConvertFromUtf32(int.Parse(s.Substring(2), NumberStyles.HexNumber));



回答3:


I have recently push my FOSS Uncode Converter at Codeplex (http://unicode.codeplex.com)

you can convert whatever you want to Hex code and from Hex code to get the right character, also there is a full information character database.

I use this code

public static char ConvertHexToUnicode(string hexCode)
    {
        if (hexCode != string.Empty)
            return ((char)int.Parse(hexCode, NumberStyles.AllowHexSpecifier));

        char empty = new char();
        return empty;
    }//end

you can see entire code on the http://unicode.codeplex.com/




回答4:


It appears you just want this in your code... you can type it as a string literal using the escape code \Uxxxxxxxx (note that this is a capital U, and there must be 8 digits). For this example, it would be: "\U0001D0EC".



来源:https://stackoverflow.com/questions/1992509/best-way-to-decode-hex-sequence-of-unicode-characters-to-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!