Convert Unicode to C #

I am trying to assign Unicode to a string, but it returns the string "Hello" as "Rose", but I need "Hello", I am converting the following function.

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

what can I do to solve this problem to return "Hello".

+6
source share
2 answers

is the Unicode character 0x041F , and its UTF-8 encoding is 0xD0 0x9F , which leads to П

Since the function returns only the input parameter, as commentators have already noted, I came to the conclusion that your original input line is in UTF-8, and you want to convert it to your own .Net line.

Where did the source line come from?

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

+8
source

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 str1 = 鿐 胑 룐 닐뗐 θ‹‘

Output str2 = Hi

+4
source

All Articles