using System; using System.Collections.Generic; using System.Text; namespace DataCollectionSystem { /// /// s十六进制和字符串互相转换 /// internal class DataConvert { /// /// byte数组转换成16进制字符串 /// /// /// public static String bytesToHexString(byte[] bArray) { StringBuilder sb = new StringBuilder(bArray.Length); string sTemp; for (int i = 0; i < bArray.Length; i++) { sTemp = bArray[i].ToString("X"); if (sTemp.Length < 2) sb.Append(0); sb.Append(sTemp.ToUpper()); } return sb.ToString(); } /// /// 将二进制值转ASCII格式十六进制字符串 /// /// 二进制值 /// 定长度的二进制 /// ASCII格式十六进制字符串 public static string toHexString(int data, int length) { string result = ""; if (data > 0) result = Convert.ToString(data, 16).ToUpper(); if (result.Length < length) { // 位数不够补0 StringBuilder msg = new StringBuilder(0); msg.Length = 0; msg.Append(result); for (; msg.Length < length; msg.Insert(0, "0")) ; result = msg.ToString(); } return result; } /// /// 将浮点数转ASCII格式十六进制字符串(符合IEEE-754标准(32)) /// /// 浮点数值 /// 十六进制字符串 public static string FloatToIntString(float data) { byte[] intBuffer = BitConverter.GetBytes(data); StringBuilder stringBuffer = new StringBuilder(0); for (int i = 0; i < intBuffer.Length; i++) { stringBuffer.Insert(0, toHexString(intBuffer[i] & 0xff, 2)); } return stringBuffer.ToString(); } /// /// 将ASCII格式十六进制字符串转浮点数(符合IEEE-754标准(32)) /// /// 16进制字符串 /// public static float StringToFloat(String data) { if (data.Length < 8 || data.Length > 8) { //throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4]; // 将16进制串按字节逆序化(一个字节2个ASCII码) for (int i = 0; i < 4; i++) { intBuffer[i] = Convert.ToByte(data.Substring((3 - i) * 2, 2), 16); } return BitConverter.ToSingle(intBuffer, 0); } } /// /// 将byte数组转为浮点数 /// /// byte数组 /// public static float ByteToFloat(byte[] bResponse) { if (bResponse.Length < 4 || bResponse.Length > 4) { //throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4]; //将byte数组的前后两个字节的高低位换过来 intBuffer[0] = bResponse[1]; intBuffer[1] = bResponse[0]; intBuffer[2] = bResponse[3]; intBuffer[3] = bResponse[2]; return BitConverter.ToSingle(intBuffer, 0); } } /// /// 从16位寄存器读出浮点数 /// /// 寄存器1值 /// 寄存器2值 /// public static float mathFloat(int x1, int x2) //x1 x2 为读取到浮点数的2个16位寄存器整型数据 { int fuhao, fuhaoRest, exponent, exponentRest; float value, weishu; fuhao = x1 / 32768; fuhaoRest = x1 % 32768; exponent = fuhaoRest / 128; exponentRest = fuhaoRest % 128; weishu = (float)(exponentRest * 65536 + x2) / 8388608; value = (float)Math.Pow(-1, fuhao) * (float)Math.Pow(2, exponent - 127) * (weishu + 1); return value; } /// /// 字符串转16进制字节数组 /// /// /// public 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; } /// /// 对字符串进行填充 /// /// 原始字符串 /// 填充后的字符串长度 /// public static string PatingStr(string txt, int len) { int k = txt.Length; for (int i = 1; i <= len - k; i++) { txt = txt.Insert(0, "0"); } return txt; } /// /// 实现任意进制的转换 /// /// 需要转换的内容 /// 原始数据类型 /// 需要转换成的数据类型 /// public static string ConvertString(string value, int fromBase, int toBase) { int intValue = Convert.ToInt32(value, fromBase); return Convert.ToString(intValue, toBase); } /// /// 将字节转换成16进制 /// /// 字节数据 /// 16进制字节 public static string ByteArrayToHexString(byte[] data) { StringBuilder sb = new StringBuilder(data.Length * 3); foreach (byte b in data) sb.Append(Convert.ToString(b, 16).PadLeft(2, '0').PadRight(3, ' ')); return sb.ToString().ToUpper(); } /// /// 获取10进制数的ASIIC码 /// /// 十进制数(0-255) /// 范围标准的2个byte数组的asiic值 public static byte[] GetAsciiBytes(int number) { string hStr = number.ToString("X2"); char[] charBuffer = hStr.ToCharArray(); byte[] asiicBuffer = new byte[2]; asiicBuffer[0] = (byte)(int)charBuffer[0]; asiicBuffer[1] = (byte)(int)charBuffer[1]; return asiicBuffer; } /// ///获取asiic码数组转byte数组 /// /// asiic数组 /// byte数组 public static byte[] GetByteByAsciiArray(byte[] asciiByte) { byte[] buffer = new byte[4]; byte[] newBuffer = new byte[4]; for (int i = 0; i < 4; i++) { string str1 = asciiByte[i * 2].ToString(); str1 = Chr(Int32.Parse(str1)); string str2 = asciiByte[i * 2 + 1].ToString(); str2 = Chr(Int32.Parse(str2)); str1 = "0X" + str1 + str2; buffer[i] = (byte)Int32.Parse(ConvertString(str1, 16, 10)); } newBuffer[0] = buffer[2]; newBuffer[1] = buffer[3]; newBuffer[2] = buffer[0]; newBuffer[3] = buffer[1]; return newBuffer; } /// /// 将ASIIC码转换成相对应字符 /// /// asiic码值 /// Asiic对应的字符 public static string Chr(int asciiCode) { if (asciiCode >= 0 && asciiCode <= 255) { System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding(); byte[] byteArray = new byte[] { (byte)asciiCode }; string strCharacter = asciiEncoding.GetString(byteArray); return (strCharacter); } else { throw new Exception("ASCII Code is not valid."); } } /// /// 将byte数组转为浮点数 /// /// byte数组 /// public static float ByteToFloatFIFO(byte[] bResponse) { if (bResponse.Length < 4 || bResponse.Length > 4) { //throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4]; //将byte数组的前后两个字节的高低位换过来 intBuffer[0] = bResponse[0]; intBuffer[1] = bResponse[1]; intBuffer[2] = bResponse[2]; intBuffer[3] = bResponse[3]; return BitConverter.ToSingle(intBuffer, 0); } } public static float ByteToFloat2(byte[] bResponse) { if (bResponse.Length < 4 || bResponse.Length > 4) { //throw new NotEnoughDataInBufferException(data.length(), 8); return 0; } else { byte[] intBuffer = new byte[4]; //将byte数组的前后两个字节的高低位换过来 intBuffer[0] = bResponse[3]; intBuffer[1] = bResponse[2]; intBuffer[2] = bResponse[1]; intBuffer[3] = bResponse[0]; return BitConverter.ToSingle(intBuffer, 0); } } } }