简体   繁体   English

将文本转换为unicode字符串

[英]Conversion of text to unicode strings

I have to process JSON files that looks like this: 我必须处理如下所示的JSON文件:

\u0432\u043b\u0430\u0434\u043e\u043c <b>\u043f\u0443\u0442\u0438\u043c<\/b> \u043d\u0430\u0447

Unfortunately, I'm not sure how this encoding is called. 不幸的是,我不确定如何调用此编码。

I would like to convert it to .NET Unicode strings. 我想将其转换为.NET Unicode字符串。 What's the easies way to do it? 简便的方法是什么?

This is Unicode characters for Russian alphabet. 这是俄语字母的Unicode字符。 try simply put this line in VisualStudio and it will parse it. 尝试简单地将此行放在VisualStudio中,它将对其进行解析。

string unicodeString = "\u0432\u043b\u0430\u0434\u043e\u043c";

Or if you want to convert this string to another encoding, for example utf8, try this code: 或者,如果您想将此字符串转换为其他编码,例如utf8,请尝试以下代码:

static void Main()
    {
        string unicodeString = "\u0432\u043b\u0430\u0434\u043e\u043c <b>\u043f\u0443\u0442\u0438\u043c<\b> \u043d\u0430\u0447";
        // Create two different encodings.
        Encoding utf8 = Encoding.UTF8;
        Encoding unicode = Encoding.Unicode;

        // Convert the string into a byte[].
        byte[] unicodeBytes = unicode.GetBytes(unicodeString);

        // Perform the conversion from one encoding to the other.
        byte[] utf8Bytes = Encoding.Convert(unicode, utf8, unicodeBytes);

        // Convert the new byte[] into a char[] and then into a string.
        // This is a slightly different approach to converting to illustrate
        // the use of GetCharCount/GetChars.
        char[] asciiChars = new char[utf8.GetCharCount(utf8Bytes, 0, utf8Bytes.Length)];
        utf8.GetChars(utf8Bytes, 0, utf8Bytes.Length, asciiChars, 0);
        string asciiString = new string(asciiChars);

        // Display the strings created before and after the conversion.
        Console.WriteLine("Original string: {0}", unicodeString);
        Console.WriteLine("Ascii converted string: {0}", asciiString);
        Console.ReadKey();
    }

code taken from Convert 取自Convert的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM