相关疑难解决方法(0)

我们可以简化这个字符串编码代码

是否可以将此代码简化为更清晰/更快的形式?

StringBuilder builder = new StringBuilder();
var encoding = Encoding.GetEncoding(936);

// convert the text into a byte array
byte[] source = Encoding.Unicode.GetBytes(text);

// convert that byte array to the new codepage. 
byte[] converted = Encoding.Convert(Encoding.Unicode, encoding, source);

// take multi-byte characters and encode them as separate ascii characters 
foreach (byte b in converted)
    builder.Append((char)b);

// return the result
string result = builder.ToString();
Run Code Online (Sandbox Code Playgroud)

简单地说,它需要一个带有中文字符的字符串,如郓,并将它们转换为ài.

例如,十进制中的中文字符为十六进制的37126或0x9106.

请参见http://unicodelookup.com/#0x9106/1

转换为字节数组,得到[145,6](145*256 + 6 = 37126).当在CodePage 936(简体中文)中编码时,我们得到[224,105].如果我们将这个字节数组分解为单个字符,我们224 = e0 =à和105 = 69 = i在unicode中.

请参见 …

c# optimization character-encoding

9
推荐指数
2
解决办法
9591
查看次数

标签 统计

c# ×1

character-encoding ×1

optimization ×1