Ejemplo n.º 1
0
        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);
            }
        }