C#中的Unicode到字符串转换

Bob*_*Bob 4 .net c# vb.net

如何将Unicode值转换为其等效字符串?

例如,我有"మెశమెశ",我需要一个接受此Unicode值并返回一个字符串的函数.

我正在查看System.Text.Encoding.Convert()函数,但是它没有接受Unicode值; 它需要两个编码和一个字节数组.

我基本上有一个字节数组,我需要保存在字符串字段中,然后再返回并将字符串转换回字节数组.

所以我使用ByteConverter.GetString(byteArray)将字节数组保存到字符串中,但我无法将其恢复为字节数组.

Pie*_*kel 5

请尝试以下方法:

byte[] bytes = ...;

string convertedUtf8 = Encoding.UTF8.GetString(bytes);
string convertedUtf16 = Encoding.Unicode.GetString(bytes); // For UTF-16
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用`GetBytes():

byte[] bytesUtf8 = Encoding.UTF8.GetBytes(convertedUtf8);
byte[] bytesUtf16 = Encoding.Unicode.GetBytes(convertedUtf16);
Run Code Online (Sandbox Code Playgroud)

Encoding课堂上,如果您需要它们,还有更多变种.


And*_*rew 5

使用.ToString();

this.Text = ((char)0x00D7).ToString();
Run Code Online (Sandbox Code Playgroud)