在串口通讯过程中,经常要用到 16进制与字符串、字节数组之间的转换
http://blog.csdn.net/feishanm/archive/2008/10/20/3112716.aspx
字符串转16进制字节数组
字节数组转16进制字符串
从汉字转换到16进制
从16进制转换成汉字
for (int i = 0; i < bytes.Length; i++)
{
try
{
// 每两个字符是一个 byte。
bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
System.Globalization.NumberStyles.HexNumber);
}
catch
{
// Rethrow an exception with custom message.
throw new ArgumentException("hex is not a valid hex number!", "hex");
}
}
System.Text.Encoding chs = System.Text.Encoding.GetEncoding(charset);
return chs.GetString(bytes);
}