public override byte[] Encode(ISOComponent component) { try { // if data is represented as byte[], we need to convert it to ASCII, otherwise take it as is String data; if (component.Value is byte[]) { data = System.Text.ASCIIEncoding.ASCII.GetString((byte[])component.Value); } else { data = component.Value.ToString(); } // Check the length if (data.Length > Length) { throw new ISOException(string.Format("{0}: Field length {1} is too long. Max {2}", component.Key, data.Length, Length)); } // Pad, Prefix and Translate string paddedData = _padder.Pad(data, Length); byte[] rawData = new byte[_prefix.EncodedLength + _translator.EncodedLength(Length)]; _prefix.EncodeLength(paddedData.Length, rawData); _translator.Translate(paddedData, rawData, _prefix.EncodedLength); return(rawData); } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }
public override byte[] Encode(ISOComponent component) { try { byte[] data = component.GetBytes(); // Check length int encodedLength = _prefix.EncodedLength; if (encodedLength == 0) { if (data.Length != Length) { throw new ISOException(string.Format("Binary data length is not the same as encoder length ({0}/{1}", data.Length, Length)); } } byte[] retValue = new byte[_translator.EncodedLength(data.Length) + encodedLength]; _prefix.EncodeLength(data.Length, retValue); _translator.Translate(data, retValue, encodedLength); return(retValue); } catch (Exception e) { throw new ISOException(string.Format("Exception encoding field {0}", component.Key), e); } }