HEX字符串到中文字符串 [英] HEX String to Chinese String

查看:951
本文介绍了HEX字符串到中文字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //十六进制转换为ASCII转换
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();
}

输入hexString = D3FCC4A7B6FABBB7



output return =魔§¶···



我需要的输出是狱魔耳环,但是我得到了Óüħ¶ú»。
如何让它显示正确的字符串?

解决方案

首先,将十六进制字符串转换为 byte [] ,例如在如何使用代码你将字节数组转换为十六进制字符串,反之亦然?。然后使用 System.Text.Encoding.Unicode.GetString(myArray)(使用正确的编码,可能不是Unicode,但从你的例子来看它是一个16位编码,顺便说一句,它不是ASCII,即7位)将其转换为字符串。


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[], 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.

这篇关于HEX字符串到中文字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆