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);
本文来自博客园,作者:jevan,转载请注明原文链接:https://www.cnblogs.com/DoNetCShap/p/13150676.html