/// <summary> /// Contructor of Exception /// </summary> /// <param name="tagAttr">Tag Attribute Data</param> /// <param name="innerEx">Inner exception details</param> /// <param name="exMessage">error message</param> public ParseTagException(ITagProperties tagAttr, string exMessage, Exception innerEx) : base(exMessage, innerEx) { TagData = tagAttr; }
internal static byte[] GetTagBytes(this ITagProperties tagProperties, string tagValue) { var tagFieldBytes = new List <byte>(); var tagLentgh = tagValue.Length; if (tagProperties.DataType == DataType.BCD) { tagLentgh = tagLentgh % 2 == 0 ? tagLentgh / 2 : (tagLentgh / 2) + 1; } try { if (tagProperties.IsTLV) { tagFieldBytes.AddRange(tagProperties.TagName.FromASCIIToBytes(tagProperties.Encoding)); } else { tagLentgh = tagLentgh + tagProperties.TagName.Length; } switch (tagProperties.LenDataType) { case DataType.ASCII: tagFieldBytes.AddRange(tagLentgh.ToString().PadLeft(tagProperties.LenBytesLen, '0').FromASCIIToBytes(tagProperties.Encoding)); break; case DataType.HEX: var intLen = int.Parse(tagLentgh.ToString().PadLeft(tagProperties.LenBytesLen, '0')); tagFieldBytes.AddRange(intLen.IntToHexValue(tagProperties.LenBytesLen).FromASCIIToBytes(tagProperties.Encoding)); break; case DataType.BCD: var valueLenBCD = tagLentgh.ToString().PadLeft(tagProperties.LenBytesLen, '0').ConvertToBinaryCodedDecimal(false, tagProperties.LenBytesLen); tagFieldBytes.AddRange(valueLenBCD); break; default: throw new BuildTagException(tagProperties, $"Cannot Build Length value for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}"); } if (!tagProperties.IsTLV) { tagFieldBytes.AddRange(tagProperties.TagName.FromASCIIToBytes(tagProperties.Encoding)); } switch (tagProperties.DataType) { case DataType.BIN: tagFieldBytes.AddRange(tagValue.ToBinaryStringFromHex().ToBytesFromBinaryString()); break; case DataType.BCD: var bcdValue = tagValue.ConvertToBinaryCodedDecimal(false); tagFieldBytes.AddRange(bcdValue); break; case DataType.ASCII: case DataType.HEX: tagFieldBytes.AddRange(tagValue.FromASCIIToBytes(tagProperties.Encoding)); break; default: throw new BuildTagException(tagProperties, $"Cannot Build value {tagValue} for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}"); } } catch (Exception ex) { throw new BuildTagException(tagProperties, $"Cannot Build value {tagValue} for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}", ex); } return(tagFieldBytes.ToArray()); }
/// <summary> /// Get Tag Value of Custom Field /// </summary> /// <param name="tagProperties">Tag Properties object</param> /// <param name="fieldBytes">bytes of Custom Field</param> /// <param name="currentPos">current position</param> /// <returns>Tag Name and Tag Value Tuple</returns> internal static (string, string) GetTagValue(this ITagProperties tagProperties, byte[] fieldBytes, ref int currentPos) { var tagName = string.Empty; var fieldValue = string.Empty; var lengthBytes = tagProperties.LenBytesLen; try { if (tagProperties.IsTLV) { tagName = fieldBytes.Skip(currentPos).Take(2).ToASCIIString(tagProperties.Encoding); currentPos = currentPos + 2; } int fieldLen; switch (tagProperties.LenDataType) { case DataType.ASCII: var lenValue = fieldBytes.Skip(currentPos).Take(lengthBytes).ToASCIIString(tagProperties.Encoding); fieldLen = string.IsNullOrEmpty(lenValue) ? 0 : int.Parse(lenValue); break; case DataType.HEX: var lenValueH = fieldBytes.Skip(currentPos).Take(lengthBytes).ToASCIIString(tagProperties.Encoding); fieldLen = string.IsNullOrEmpty(lenValueH) ? 0 : lenValueH.HexValueToInt(); break; case DataType.BCD: var lenValueBCD = fieldBytes.Skip(currentPos).Take(lengthBytes).ToArray().BDCToString(); fieldLen = string.IsNullOrEmpty(lenValueBCD) ? 0 : int.Parse(lenValueBCD); break; default: throw new ParseTagException(tagProperties, $"Cannot Parse Length value for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}"); } if (!tagProperties.IsTLV) { currentPos = currentPos + lengthBytes; tagName = fieldBytes.Skip(currentPos).Take(2).ToASCIIString(tagProperties.Encoding); fieldLen = fieldLen - 2; currentPos = currentPos + 2; } else { currentPos = currentPos + lengthBytes; } switch (tagProperties.DataType) { case DataType.BIN: fieldValue = fieldBytes.Skip(currentPos).Take(fieldLen).ToStringFromBinary(); currentPos = currentPos + fieldLen; break; case DataType.ASCII: fieldValue = fieldBytes.Skip(currentPos).Take(fieldLen).ToASCIIString(tagProperties.Encoding); currentPos = currentPos + fieldLen; break; case DataType.HEX: fieldValue = fieldBytes.Skip(currentPos).Take(fieldLen).HexBytesToString(); currentPos = currentPos + fieldLen; break; case DataType.BCD: fieldValue = fieldBytes.Skip(currentPos).Take(fieldLen).ToArray().BDCToString(); currentPos = currentPos + fieldLen; break; default: throw new ParseTagException(tagProperties, $"Cannot Parse Field value for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}"); } } catch (Exception ex) { throw new ParseTagException(tagProperties, $"Cannot Parse Field value for {tagProperties?.TagName} and Len Type {tagProperties?.LenDataType}", ex); } return(tagName, fieldValue); }
/// <summary> /// Contructor of Exception /// </summary> /// <param name="tagAttr">Tag Attribute Data</param> /// <param name="exMessage">error message</param> public BuildTagException(ITagProperties tagAttr, string exMessage) : base(exMessage) { TagData = tagAttr; }