C# 解析16进制字符串。将16进制字符串转换成明文字符串
将诸如 字符串 “73 6F 75 6E 64 34 2F 64 65 6D 6F 31 32 38 2E 6D 70 33 00” 转换成明文的方法
方法定义:
private static string HexStringToString(string hexString, Encoding encoding = null) { if (string.IsNullOrEmpty(hexString)) return string.Empty; if (encoding == null) encoding = Encoding.ASCII; string[] byteitem = hexString.Trim().Split(' '); List<byte> lstByte = new List<byte>(); foreach (string item in byteitem) lstByte.Add(Convert.ToByte(item, 16)); return encoding.GetString(lstByte.ToArray()); }
调用时:
string hexString = "73 6F 75 6E 64 34 2F 64 65 6D 6F 31 32 38 2E 6D 70 33 00"; Console.WriteLine(HexStringToString(hexString)); //转换结果:sound4/demo128.mp3