Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

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 "Привет".

like image 702
manoj Avatar asked Jan 28 '13 10:01

manoj


Video Answer


1 Answers

П 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).

like image 184
devio Avatar answered Oct 25 '22 20:10

devio