简体   繁体   中英

HEX String to Chinese String

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?

First, convert the hex string to a byte[] , eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM