/// <summary> /// 构造TLV结构 /// </summary> /// <param name="tag">标签</param> /// <param name="len">长度</param> /// <param name="val">值</param> public TLV_Structure(C_Global.CEnum.TagName tag, C_Global.CEnum.TagFormat format, uint len, byte[] val) { this.m_Tag = tag; this.m_TagFormat = format; this.m_uiValueLen = len; this.m_bValueBuffer = new byte[this.m_uiValueLen]; this.init(len, val); }
public TagStruct(C_Global.CEnum.TagName _tag, C_Global.CEnum.TagFormat _format, uint _len, byte[] _tag_buf) { tag = _tag; format = _format; len = _len; tag_buf = new byte[len]; tag_buf = _tag_buf; }
public void AddTagKey(C_Global.CEnum.TagName tag, C_Global.CEnum.TagFormat style, uint len, byte[] val) { int i = 0; for (i = 0; i < structLen; i++) { if (null == m_tagList[i]) { break; } } m_tagList[i] = new TagStruct(tag, style, len, val); }
/// <summary> /// 格式化数据类型 /// </summary> /// <param name="format">数据类型</param> /// <returns></returns> public string FormatString(C_Global.CEnum.TagFormat format) { string sDest = null; switch (format) { case C_Global.CEnum.TagFormat.TLV_INTEGER: sDest = toInteger().ToString(); break; case C_Global.CEnum.TagFormat.TLV_DATE: sDest = (toDate() == new DateTime())?"Date Paser Error":toDate().ToString("yyyy-MM-dd"); break; case C_Global.CEnum.TagFormat.TLV_TIME: sDest = (toTime() == new DateTime())?"Time Paser Error":toTime().ToString("hh:mm:ss"); break; case C_Global.CEnum.TagFormat.TLV_TIMESTAMP: sDest = (toTimeStamp() == new DateTime())?"DateTime Paser Error":toTimeStamp().ToString("yyyy-MM-dd hh:mm:ss"); break; case C_Global.CEnum.TagFormat.TLV_STRING: sDest = toString(); break; case C_Global.CEnum.TagFormat.TLV_MONEY: sDest = toMoney().ToString("C"); break; case C_Global.CEnum.TagFormat.TLV_NUMBER: sDest = toNumber().ToString(); break; case C_Global.CEnum.TagFormat.TLV_EXTEND: sDest = (toExtend() == null)?"(NULL)":"(" + toExtend().ToString() + ")"; break; default: sDest = "ERROR:Not find TagFormat to format value"; break; } return(sDest); }
/// <summary> /// 转换成byte /// </summary> /// <param name="tagformat">数据类型</param> /// <param name="val">值</param> /// <returns></returns> public static byte[] ValueToByteArray(C_Global.CEnum.TagFormat tagformat, object val) { switch (tagformat) { case C_Global.CEnum.TagFormat.TLV_DATE: { if (val.GetType() != typeof(System.DateTime)) { throw new TypeException(val.GetType().ToString(), typeof(System.DateTime).ToString()); } int year = ((DateTime)val).Year - 1900; int month = ((DateTime)val).Month; int day = ((DateTime)val).Day; return(new byte[] { (byte)year, (byte)month, (byte)day }); } case C_Global.CEnum.TagFormat.TLV_EXTEND: { if (val.GetType() != typeof(Packet_Body)) { throw new TypeException(val.GetType().ToString(), typeof(Packet_Body).ToString()); } else if (((Packet_Body)val).m_Status != CEnum.Body_Status.MSG_STRUCT_OK) { throw new TypeException(((Packet_Body)val).m_Status.ToString(), CEnum.Body_Status.MSG_STRUCT_OK.ToString()); } return(((Packet_Body)val).ToByteArray()); } case C_Global.CEnum.TagFormat.TLV_INTEGER: { byte[] ret = null; System.Type type = val.GetType(); if (type == typeof(ulong)) { ulong val_int = (ulong)val; ret = new byte[] { (byte)val_int, (byte)(val_int >> 8), (byte)(val_int >> 8 * 2), (byte)(val_int >> 8 * 3), (byte)(val_int >> 8 * 4), (byte)(val_int >> 8 * 5), (byte)(val_int >> 8 * 6), (byte)(val_int >> 8 * 7) }; } else if (type == typeof(long)) { ulong val_int = (ulong)((long)val); ret = new byte[] { (byte)val_int, (byte)(val_int >> 8), (byte)(val_int >> 8 * 2), (byte)(val_int >> 8 * 3), (byte)(val_int >> 8 * 4), (byte)(val_int >> 8 * 5), (byte)(val_int >> 8 * 6), (byte)(val_int >> 8 * 7) }; } else if (type == typeof(uint)) { uint val_int = (uint)val; ret = new byte[] { (byte)val_int, (byte)(val_int >> 8), (byte)(val_int >> 16), (byte)(val_int >> 24) }; } else if (type == typeof(int)) { uint val_int = (uint)((int)val); ret = new byte[] { (byte)val_int, (byte)(val_int >> 8), (byte)(val_int >> 16), (byte)(val_int >> 24) }; } else if (type == typeof(short)) { short val_int = (short)val; ret = new byte[] { (byte)val_int, (byte)(val_int >> 8) }; } else if (type == typeof(ushort)) { ushort val_int = (ushort)val; ret = new byte[] { (byte)val_int, (byte)(val_int >> 8) }; } else if (type == typeof(byte)) { byte val_int = (byte)val; ret = new byte[] { (byte)val_int }; } else if (type == typeof(sbyte)) { byte val_int = (byte)((sbyte)val); ret = new byte[] { (byte)val_int }; } else { throw new TypeException(val.GetType().ToString(), "ulong,long,uint,int,byte,sbyte"); } return(ret); } case C_Global.CEnum.TagFormat.TLV_MONEY: { if (val.GetType() != typeof(double)) { throw new TypeException(val.GetType().ToString(), typeof(double).ToString()); } uint val_int = (uint)((double)val); uint val_dec = (uint)(((double)val - val_int) * 100); return(new byte[] { (byte)(val_int >> 16), (byte)(val_int >> 8), (byte)val_int, (byte)val_dec }); } case C_Global.CEnum.TagFormat.TLV_NUMBER: { if (val.GetType() != typeof(string)) { throw new TypeException(val.GetType().ToString(), typeof(string).ToString()); } string val_string = (string)val; if (!System.Text.RegularExpressions.Regex.IsMatch(val_string, @"^\d{11,12}$", System.Text.RegularExpressions.RegexOptions.Compiled)) { throw new ValueException(val_string, "11-12位的数字"); } byte[] ret = new byte[1 + (val_string.Length + 1) / 2]; ret[0] = (byte)val_string.Length; for (int i = 0; i < val_string.Length; i++) { if (i % 2 == 0) { ret[1 + i / 2] += (byte)(int.Parse(val_string.Substring(i, 1)) << 4); } else { ret[1 + i / 2] += (byte)(int.Parse(val_string.Substring(i, 1))); } } return(ret); } case C_Global.CEnum.TagFormat.TLV_STRING: { if (val.GetType() != typeof(string)) { throw new TypeException(val.GetType().ToString(), typeof(string).ToString()); } return(System.Text.Encoding.Default.GetBytes((string)val)); } case C_Global.CEnum.TagFormat.TLV_TIME: { if (val.GetType() != typeof(System.DateTime)) { throw new TypeException(val.GetType().ToString(), typeof(System.DateTime).ToString()); } int hour = ((DateTime)val).Hour; int minute = ((DateTime)val).Minute; int second = ((DateTime)val).Second; return(new byte[] { (byte)hour, (byte)minute, (byte)second }); } case C_Global.CEnum.TagFormat.TLV_TIMESTAMP: { if (val.GetType() != typeof(System.DateTime)) { throw new TypeException(val.GetType().ToString(), typeof(System.DateTime).ToString()); } int year = ((DateTime)val).Year - 1900; int month = ((DateTime)val).Month; int day = ((DateTime)val).Day; int hour = ((DateTime)val).Hour; int minute = ((DateTime)val).Minute; int second = ((DateTime)val).Second; return(new byte[] { (byte)year, (byte)month, (byte)day, (byte)hour, (byte)minute, (byte)second }); } case C_Global.CEnum.TagFormat.TLV_BOOLEAN: { if (val.GetType() != typeof(bool)) { throw new TypeException(val.GetType().ToString(), typeof(bool).ToString()); } bool val_int = (bool)val; return(new byte[] { Convert.ToByte(val_int) }); } default: throw new ValueException(val.ToString(), "TagFormat定义的所有类型"); } }