using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BatCharging.Service { public class ByteUtils { /// /// 十六进制字符串转字符串 /// /// /// public static string StrHex2Str(string strHex) { byte[] keyWord = new byte[strHex.Length / 2]; string strResult = ""; for (int i = 0; i < keyWord.Length; i++) { try { keyWord[i] = (byte)(0xff & Convert.ToInt32(strHex.Substring(i * 2, 2), 16)); strResult += Convert.ToChar(keyWord[i]).ToString(); } catch (Exception e) { e.ToString(); } } return strResult; } /// /// 字符串转16进制字符串 /// /// /// public static string Str2HexStr(string strSrc) { char[] chTemp = strSrc.ToCharArray(); string strResult = ""; foreach (var ch in chTemp) { int value = Convert.ToInt32(ch); strResult += String.Format("{0:X}", value); } //strResult = "6000" + strResult; return strResult; } /// /// 根据高位优先的原理的两字节数据解析返回一个16为无符号整数 /// /// 原字节数组(含有两字节) /// 返回无符号整数 public static ushort ToUInt16(byte[] value) { return (ushort)((value[0] << 8) | (value[1] & 0x00FF)); } /// /// 根据高位优先的原理的两字节数据解析返回一个16为无符号整数 /// /// 原字节数组 /// 缓冲区偏移 /// 返回无符号整数 public static UInt16 ToUInt16(byte[] value, int offset) { return (ushort)((value[offset] << 8) | (value[offset + 1] & 0x00FF)); } /// /// 根据高位优先颠倒的原理的两字节数据解析返回一个16为无符号整数 /// /// 原字节数组 /// 缓冲区偏移 /// 返回无符号整数 public static UInt16 DToUInt16(byte[] value, int offset) { return (ushort)(value[offset] + (value[offset + 1] << 8)); } public static UInt32 ToUInt32(byte[] value, int offset) { UInt16 low = (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF)); UInt16 height = (ushort)((value[offset + 3] << 8) | (value[offset + 2] & 0x00FF)); return (UInt32)(height << 16 | low); } public static UInt32 DToUInt32(byte[] value, int offset) { UInt16 low = (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF)); UInt16 height = (ushort)((value[offset + 3] << 8) | (value[offset + 2] & 0x00FF)); return (UInt32)(height << 16 | low); } /// /// 16进制数高地位对调 /// /// /// public static byte[] Int16ToByteArray(Int16 value) //16进制数高地位对调 { byte[] bytesResult = new byte[2]; byte[] temp = new byte[2]; BitConverter.GetBytes(Convert.ToInt16(value)).CopyTo(temp, 0); bytesResult[0] = temp[1]; bytesResult[1] = temp[0]; return bytesResult; } /// /// 32进制数高地位对调 /// /// /// public static byte[] Int32ToByteArray(Int32 value) //16进制数高地位对调 { byte[] bytesResult = new byte[4]; byte[] temp = new byte[4]; BitConverter.GetBytes(Convert.ToInt32(value)).CopyTo(temp, 0); bytesResult[0] = temp[3]; bytesResult[1] = temp[2]; bytesResult[2] = temp[1]; bytesResult[3] = temp[0]; return bytesResult; } /// /// 获取16进制字符串的字节数组 /// /// hexString 16进制字符串 /// 字节数组 public static byte[] ToByteByHexStr(string hexString) { if (hexString == null) return null; 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; } /// /// 获取32进制字符串的字节数组 /// /// hexString 16进制字符串 /// 字节数组 public static byte[] ToByteByHexStr32(string hexString, int leng) { byte[] bytes = new byte[leng]; // 使用UTF8编码将字符串转换为字节数组 byte[] byteArray = Encoding.ASCII.GetBytes(hexString); // 如果需要确保数组长度为32字节,可以添加逻辑来处理长度不足的情况 if (byteArray.Length < leng) { Array.Copy(byteArray, 0, bytes, 0, byteArray.Length); } //else if (byteArray.Length > leng) //{ // // 长度超过32字节,可以截断或者抛出异常 // Array.Copy(byteArray, 0, new byte[leng], 0, leng); // 截断数组到32字节 //} return bytes; } /// /// 获取16进制字符串的字节数组 /// /// hexString 16进制字符串 /// 字节数组 public static byte[] ToByteByStr(string strValue) { #region 转化到十六进制字符串 char[] chTemp = strValue.ToCharArray(); string strHex = ""; foreach (var ch in chTemp) { int value = Convert.ToInt32(ch); strHex += String.Format("{0:X}", value); } #endregion 转化到十六进制字符串 if (strHex == null) return null; strHex = strHex.Replace(" ", ""); if ((strHex.Length % 2) != 0) strHex += " "; byte[] returnBytes = new byte[strHex.Length / 2]; for (int i = 0; i < returnBytes.Length; i++) returnBytes[i] = Convert.ToByte(strHex.Substring(i * 2, 2), 16); return returnBytes; } public static string BytesToHexStr(byte[] data) { StringBuilder sb = new StringBuilder(data.Length * 3); foreach (byte b in data) sb.Append(Convert.ToString(b, 16).PadLeft(2, '0') + " "); return sb.ToString().ToUpper(); } /// /// 字节转换成布尔数组 /// /// /// public static bool[] ByteToBoolArray(byte value) { bool[] array = new bool[8]; for (int i = 0; i <= 7; i++) { //对于byte的每bit进行判定 array[i] = (value & 1) == 1; //判定byte的最后一位是否为1,若为1,则是true;否则是false value = (byte)(value >> 1); //将byte右移一位 } return array; } /// /// UInt16转换成布尔数组 /// /// 16位无符号整形 /// 布尔数组 public static bool[] UInt16ToBoolArray(ushort value) { bool[] array = new bool[16]; for (int i = 0; i <= 15; i++) { //对于byte的每bit进行判定 array[i] = (value & 1) == 1; //判定byte的最后一位是否为1,若为1,则是true;否则是false value = (ushort)(value >> 1); //将byte右移一位 } return array; } public static string CP56time2aToStr(byte[] bytes) { return "20" + Convert.ToInt32(bytes[6]).ToString("00") + "-" + Convert.ToInt32(bytes[5]).ToString("00") + "-" + Convert.ToInt32(bytes[4]).ToString("00") + " " + Convert.ToInt32(bytes[3]).ToString("00") + ":" + Convert.ToInt32(bytes[2]).ToString("00") + ":" + (ToFUInt16(bytes, 0) / 1000).ToString("00"); } public static ushort ToFUInt16(byte[] value, int offset) { return (ushort)((value[offset + 1] << 8) | (value[offset] & 0x00FF)); } /// /// BCD码转byte /// /// /// public static byte BCDToByte(byte value) { //高四位 byte high = (byte)((value >> 4 & 0xF)); //低四位 byte low = (byte)(value & 0xF); return (byte)(high * 10 + low); } /// /// byte转BCD码 /// /// /// public static byte ByteToBCD(byte value) { //高四位 byte high = (byte)(value / 10); //低四位 byte low = (byte)(value % 10); return (byte)(high << 4 | low); } } }