HEX String to Chinese String

China☆狼群 提交于 2019-12-02 11:35:51

问题


I have the following code to convert from HEX to ASCII.

//Hexadecimal to ASCII Convertion
private static string hex2ascii(string hexString)
{
    MessageBox.Show(hexString);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i <= hexString.Length - 2; i += 2)
    {
        sb.Append(Convert.ToString(Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber))));
    }
    return sb.ToString();
}

input hexString = D3FCC4A7B6FABBB7

output return = Óüħ¶ú»·

The output that I need is 狱魔耳环, but I am getting Óüħ¶ú»· instead. How would I make it display the correct string?


回答1:


First, convert the hex string to a byte[], e.g. using code at How do you convert Byte Array to Hexadecimal String, and vice versa?. Then use System.Text.Encoding.Unicode.GetString(myArray) (use proper encoding, might not be Unicode, but judging from your example it is a 16-bit encoding, which, incidentally, is not "ASCII", which is 7-bit) to convert it to a string.



来源:https://stackoverflow.com/questions/10419279/hex-string-to-chinese-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!