Unicode Conversion in c#

对着背影说爱祢 提交于 2019-11-28 07:46:31

问题


i am trying to assign Unicode on string but it return "Привет" string as "Привет" But i need "Привет", i am converting by following function .

public string Convert(string str)
{
    byte[] utf8Bytes = Encoding.UTF8.GetBytes(str);
    str = Encoding.UTF8.GetString(utf8Bytes);
    return str;
}

what can i do for solve this problem to return "Привет".


回答1:


П is Unicode character 0x041F, and its UTF-8 encoding is 0xD0 0x9F resulting in П.

Since the function only returns the input parameter, as commenters already discussed, I conclude that your original input string is actually in UTF-8, and you want to convert it into native .Net string.

Where does the original string come from?

Instead of reading the input into a C# string, change your code to read a byte[], and then call Encoding.UTF8.GetString(inputUtf8ByteArray).




回答2:


I Tried the following code below and these were my results:

        string test="Привет";
        byte[] utf8Bytes = Encoding.UTF8.GetBytes(test);

        String str1 = Encoding.Unicode.GetString(utf8Bytes);
        String str2 = Encoding.UTF8.GetString(utf8Bytes);

Output of str1=鿐胑룐닐뗐苑

Output of str2=Привет



来源:https://stackoverflow.com/questions/14560223/unicode-conversion-in-c-sharp

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