貼一個找到的現成16進制互轉方法
首先先來看數據源
C1 C1 17 11 2B 00 08 D9 6B 30 01 00 01 00 00 1E 84 80 13 88 00 00 00 00 00 00 00 03 00 01 00 0F 42 40 00 00 00 55 01 01 00 00 00 00 00 00 00 00 00 00
這是一個16進制的字符串,具體怎么切割操作就不說了,直接上轉換代碼
byte.Parse(
tempArray[i], System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture.NumberFormat);
上面那個方法好像有點點問題,又找了一個能用的,放這里
private static byte[] strToToHexByte(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), 16); return returnBytes; }
這樣就可以了
那么16進制byte[] 轉回字符串呢?
string hex = BitConverter.ToString(tempBytes, 0, tempBytes.Length).Replace("-", string.Empty);
結束