using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace GPSBusiness.Helper { public class ByteConvert { public static System.Text.Encoding GB2312 = System.Text.Encoding.GetEncoding("gb2312"); public static string ToString(byte[] buffer) { return GB2312.GetString(buffer); } public static string ToString(byte[] buffer, int index, int length) { return GB2312.GetString(ToBytes(buffer, index, length)); } public static Int16 ToInt16(byte[] buffer, int index) { byte[] newbyte = ToBytes(buffer, index, 2); Array.Reverse(newbyte); return BitConverter.ToInt16(newbyte, 0); } public static UInt16 ToUInt16(byte[] buffer, int index) { byte[] newbyte = ToBytes(buffer, index, 2); Array.Reverse(newbyte); return BitConverter.ToUInt16(newbyte, 0); } public static Int32 ToInt32(byte[] buffer, int index) { byte[] newbyte = ToBytes(buffer, index, 4); Array.Reverse(newbyte); return BitConverter.ToInt32(newbyte, 0); } public static UInt32 ToUInt32(byte[] buffer, int index) { byte[] newbyte = ToBytes(buffer, index, 4); Array.Reverse(newbyte); return BitConverter.ToUInt32(newbyte, 0); } public static byte[] ToBytes(byte[] buffer, int index, int length) { try { byte[] newbyte = new byte[length]; Array.Copy(buffer, index, newbyte, 0, length); return newbyte; } catch (Exception E) { return null; } } public static byte[] ToBytes(byte[] buffer, int length) { int index = 0; if (buffer.Length < length) { index = length - buffer.Length; } return ToBytes(buffer, index, length); } public static byte[] ToBytes(string value) { return GB2312.GetBytes(value); } public static byte[] ToBytes(string value, int index, int length) { if (value == null) value = string.Empty; string strValue = value.PadRight(length, '0'); return GB2312.GetBytes(strValue.ToCharArray(), index, length); } public static byte[] ToBytes(string value, int length) { return ToBytes(value, 0, length); } public static byte[] ToBytes(Int32 value) { byte[] buffer = BitConverter.GetBytes(value); Array.Reverse(buffer); return buffer; } public static byte[] ToBytes(UInt32 value) { byte[] buffer = BitConverter.GetBytes(value); Array.Reverse(buffer); return buffer; } public static byte[] ToBytes(Int16 value) { byte[] buffer = BitConverter.GetBytes(value); Array.Reverse(buffer); return buffer; } public static byte[] ToBytes(UInt16 value) { byte[] buffer = BitConverter.GetBytes(value); Array.Reverse(buffer); return buffer; } public static UInt32 GetMdt(byte[] buffer) { byte[] byte_Simid = new byte[6]; Array.Copy(buffer, 0, byte_Simid, 0, 4); string str_Simid = ""; for (int j = 0; j < 4; j++) { int chu = byte_Simid[j] / 16; int yu = byte_Simid[j] % 16; string chuStr = chu.ToString("X"); string yuStr = yu.ToString("X"); string retStr = chuStr + yuStr; str_Simid += retStr; } return Convert.ToUInt32(str_Simid, 16); } public static byte GetCRC(byte[] buffer, int index, int length) { byte value = buffer[index]; for (int i = index + 1; i < length + index; i++) { value = (byte)(value ^ buffer[i]); } return value; } /// /// 转译去除标识位0x7e,0x7d 0x01转译为0x7d,0x7d 0x02转译为0x7e /// /// /// public static byte[] Fzy(byte[] buffer) { int length = buffer.Length; List list = new List(); list.Add(0x7e); for (int i = 1; i < length - 1; i++) { if (i > length) { continue; } if (buffer[i] == 0x7d) { byte bn = buffer[i + 1]; if (bn == 0x01) { list.Add(0x7d); i++; } else if (bn == 0x02) { list.Add(0x7e); i++; } else { list.Add(buffer[i]); } } else { list.Add(buffer[i]); if (i + 5 < length) { if (buffer[i] == 0x7e && buffer[i + 1] == 0x00 && buffer[i + 2] == 0x00 && buffer[i + 3] == 0x00) { return list.ToArray(); } } } } list.Add(0x7e); return list.ToArray(); } public static byte[] Zy(byte[] buffer) { int length = buffer.Length; List list = new List(); list.Add(0x7e); for (int i = 1; i < length - 1; i++) { if (i > length) { continue; } if (buffer[i] == 0x7e) { list.Add(0x7d); list.Add(0x02); } else if (buffer[i] == 0x7d) { list.Add(0x7d); list.Add(0x01); } else { list.Add(buffer[i]); } } list.Add(0x7e); return list.ToArray(); } /// /// 字节数组转换成十六进制字符串 /// /// 要转换的字节数组 /// public static string ByteArrayToHexStr(byte[] byteArray) { if (byteArray == null) return ""; int capacity = byteArray.Length * 2; StringBuilder sb = new StringBuilder(capacity); if (byteArray != null) { for (int i = 0; i < byteArray.Length; i++) { sb.Append(byteArray[i].ToString("X2")); } } return sb.ToString(); } /// /// 数组转成十六进制字符串 /// // public static final String bytesToHexString(byte[] bArray) { // StringBuffer sb = new StringBuffer(bArray.length); // String sTemp; // for (int i = 0; i < bArray.length; i++) { // sTemp = Integer.toHexString(0xFF & bArray[i]); // if (sTemp.length() < 2) // sb.append(0); // sb.append(sTemp.toUpperCase()); // } // return sb.toString(); // } //public static String toHexString1(byte[] b) //{ // StringBuffer buffer = new StringBuffer(); // for (int i = 0; i < b.length; ++i) // { // buffer.append(toHexString1(b[i])); // } // return buffer.toString(); //} //public static String toHexString1(byte b) //{ // String s = Integer.toHexString(b & 0xFF); // if (s.length() == 1) // { // return "0" + s; // } // else // { // return s; // } //} /// /// 16进制字符串转byte数据 /// //public static byte[] HexStringToByte2(string hex) //{ // int len = (hex.Length / 2); // byte[] result = new byte[len]; // char[] achar = hex.ToCharArray(); // for (int i = 0; i < len; i++) // { // int pos = i * 2; // result[i] = (byte)(Convert.ToByte(achar[pos]) << 4 | Convert.ToByte(achar[pos + 1])); // } // return result; //} /// /// 把16进制字符串转换成字节数组 /// public static byte[] HexStringToByte(string hex) { int len = (hex.Length / 2); byte[] result = new byte[len]; char[] achar = hex.ToCharArray(); for (int i = 0; i < len; i++) { int pos = i * 2; result[i] = (byte)(ToByte(achar[pos]) << 4 | ToByte(achar[pos + 1])); } return result; } private static int ToByte(char c) { byte b = (byte)"0123456789ABCDEF".IndexOf(c); return b; } //byte[]数组转换成十六进制字符串方法2: public static byte[] GetSendBuffer(byte[] headerBytes, byte[] bodyBytes) { IntPtr ptr = IntPtr.Zero; try { int bufferSize = headerBytes.Length + bodyBytes.Length; ptr = Marshal.AllocHGlobal(bufferSize); // 拷贝包头到缓冲区首部 Marshal.Copy(headerBytes, 0, ptr, headerBytes.Length); // 拷贝包体到缓冲区剩余部分 Marshal.Copy(bodyBytes, 0, ptr + headerBytes.Length, bodyBytes.Length); byte[] bytes = new byte[bufferSize]; Marshal.Copy(ptr, bytes, 0, bufferSize); return bytes; } finally { if (ptr != IntPtr.Zero) { Marshal.FreeHGlobal(ptr); } } } /// /// 字符串转16进制字节数组 /// /// /// public static byte[] StrToToHexByte(string hexString) { hexString = hexString.Replace(" ", ""); if ((hexString.Length % 2) != 0) hexString = hexString.Insert(hexString.Length - 1, 0.ToString()); 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; } public static byte IntToBcd(int n) { // extract each digit from the input number n byte d1 = (byte) (n / 10); byte d2 = (byte) (n % 10); // combine the decimal digits into a BCD number return (byte) ((d1 << 4) | d2); } public static byte ConvertBcd(byte b)//byte转换为BCD码 { //高四位 byte b1 = (byte)(b / 10); //低四位 byte b2 = (byte)(b % 10); return (byte)((b1 << 4) | b2); } /// /// 将BCD一字节数据转换到byte 十进制数据 /// /// 字节数 /// 返回转换后的BCD码 public static byte ConvertBcdToInt(byte b) { //高四位 byte b1 = (byte)((b >> 4) & 0xF); //低四位 byte b2 = (byte)(b & 0xF); return (byte)(b1 * 10 + b2); } } }