简体   繁体   English

c#中的Unicode转换

[英]Unicode Conversion in c#

i am trying to assign Unicode on string but it return "Привет" string as "Привет" But i need "Привет", i am converting by following function . 我试图在字符串上分配Unicode但它返回“Привет”字符串为“ПривÐμÑ”,但我需要“Привет”,我通过以下函数进行转换。

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 "Привет". 我能做些什么来解决这个问题才能返回“Привет”。

П is Unicode character 0x041F , and its UTF-8 encoding is 0xD0 0x9F resulting in П . П是Unicode字符0x041F ,其UTF-8编码是0xD0 0x9F导致П

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. 由于函数只返回输入参数,正如已经讨论过的评论者所说,我得出的结论是你的原始输入字符串实际上是UTF-8,并且你想将它转换为原生的.Net字符串。

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) . 不要将输入读入C# string ,而是将代码更改为读取byte[] ,然后调用Encoding.UTF8.GetString(inputUtf8ByteArray)

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=鿐胑룐닐뗐苑 输出str1 =鿐胑룐닐뗐苑

Output of str2=Привет 输出str2 =Привет

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

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