标签:
/// <summary> /// 解析报文,适用-协议只有头部标记 /// </summary> /// <param name="rawData">原始报文</param> /// <param name="headCode">报文头部标识</param> /// <returns></returns> public static string Solver(ref string rawDgram, string headCode) { if (string.IsNullOrEmpty(rawDgram) || rawDgram.Length <= headCode.Length) { return string.Empty; } //如果报文为空||报文的长度小于等于头部标记的长度,返回为空 int headIndex = -1; //头部索引 int endIndex = -1; //尾部索引 string tempDgram = string.Empty; headIndex = rawDgram.IndexOf(headCode); //IndexOf(headCode)-头部标记字符串在报文中的第一个匹配项的索引。 if (headIndex >= 0) //报文中包含头部字节 { endIndex = rawDgram.IndexOf(headCode, headIndex + headCode.Length); //头部标识字符串在报文中第二个匹配项的索引。 if (endIndex >= 0) //报文包含第二个头部标识字符串,说明报文中包含一条指令 { tempDgram = rawDgram.Substring(headIndex, endIndex - headIndex); rawDgram = rawDgram.Substring(endIndex); } } return tempDgram; } /// <summary> /// 将BYTE数组转换为两位的数字字符串(专用通信) /// </summary> /// <param name="bytes">要转换的BYTE数组</param> /// <param name="index">转换起始位置</param> /// <param name="size">转换长度</param> /// <returns>字符串</returns> public static string DeserializeString(byte[] bytes, int index, int size) { if (null == bytes || bytes.Length <= index) { return string.Empty; } try { StringBuilder strBuilder = new StringBuilder(); for (int i = index; i < index + size; ++i) { strBuilder.AppendFormat("{0:X2}", bytes[i]); } return strBuilder.ToString(); } catch { return null; } } /// <summary> /// 将2位的数字字符串转换为BYTE数组(专用通信) /// </summary> /// <param name="str"></param> /// <param name="index"></param> /// <param name="size"></param> /// <returns></returns> public static byte[] SerializeString(string str, int index, int size) { if (string.IsNullOrEmpty(str) || str.Length <= index || size < 0) { return null; } try { str = str.Substring(index, size); if (string.IsNullOrEmpty(str)) { return null; } int lenght = str.Length; byte[] bytes = new byte[lenght / 2]; for (int i = 0; i < lenght / 2; i++) { int btvalue = Convert.ToInt32(str.Substring(i * 2, 2), 16); bytes[i] = (byte)btvalue; } return bytes; } catch { return null; } } /// <summary> /// 将字符串转换为BYTE数组 /// </summary> /// <param name="strValue">要转换的字符串</param> /// <param name="encode">字符编码格式</param> /// <returns>BYTE数组</returns> public static Byte[] SerializeString(string strValue, int index, int size, Encoding encode) { if (string.IsNullOrEmpty(strValue) || strValue.Length <= index || size <= 0) { return null; } if (null == encode) { encode = Encoding.Default; } try { strValue = strValue.Substring(index, size); if (string.IsNullOrEmpty(strValue)) { return null; } return encode.GetBytes(strValue); } catch { return null; } } /// <summary> /// 将BYTE数组转换为字符串 /// </summary> /// <param name="bt">要转换的BYTE数组</param> /// <param name="encode">字符编码格式</param> /// <returns>字符串</returns> public static string DeserializeString(Byte[] bt, int index, int size, Encoding encode) { if (null == bt || bt.Length <= index) { return string.Empty; } if (null == encode) { encode = Encoding.Default; } try { return encode.GetString(bt, index, size); } catch { return string.Empty; } }
标签:
原文地址:http://www.cnblogs.com/shikangjie/p/5702028.html