Loading

C# 16进制与Byte[]数组互转

byte[]转16进制

public string ByteArrayToHexString(byte[] data)
{
StringBuilder sb = new StringBuilder();
foreach(byte b in data)
{
sb.Append(((int)b).ToString("X2"));
}
return sb.ToString().ToUpper();
}

  

 16进制转byte[]

private byte[] HexStrTobyte(string hexString)
{
hexString = hexString.Replace(" ", "");
if ((hexString.Length % 2) != 0)
hexString += " ";
byte[] returnBytes = new byte[hexString.Length / 2];
for (int i = 0; i < returnBytes.Length; i++)
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
return returnBytes;
}

 byte[]转ASCII字符串

1 Encoding.ASCII.GetString(buffer,0,count);
2 
3 Encoding.GetEncoding("GB2312").GetString(data);

 

posted @ 2020-06-17 09:45  jevan  阅读(2615)  评论(0编辑  收藏  举报