OpenISO8583.Net BCD fix format unpack error

481 views Asked by At

I downloaded the code from code.google and get the last version v0.5.2
I set a field in bcd fix format,which is N-6 in bcd format(bit._003_proc_code)
For ex:
*Field definition:
DefaultTemplate =new Template { { Bit._002_PAN, FieldDescriptor.BcdVar(2, 19,Formatters.Ascii) }, { Bit._003_PROC_CODE, FieldDescriptor.BcdFixed(3)}, { Bit._004_TRAN_AMOUNT, FieldDescriptor.BcdFixed(6) }, .............. }

usage:

Iso8583 msg =new Iso8584();
msg[3]="000000";

when i unpack the message ,i can only get “0000” from message 3 .
is this a bug or error in definition

1

There are 1 answers

0
Eliseo On

I would wait to hear from John Oxley to see if this is just a coding error. Now that I said that, I think it is a bug.

I had problems with the BcdFixed definition and ended up creating a new Fixed Length BCD formatter to work around the problem.

Here is what I did:

  1. I created a class called FixedLengthBcdFormatter which is a variation of the FixedLengthFormatter class.

    /// /// Fixed field formatter /// public class FixedLengthBcdFormatter : ILengthFormatter { private readonly int _packedLength; private readonly int _unPackedLength;

    ///<summary>
    ///  Fixed length field formatter
    ///</summary>
    ///<param name = "unPackedLength">The unpacked length of the field.</param>
    public FixedLengthBcdFormatter(int unPackedLength)
    {
        _unPackedLength = unPackedLength;
        double len = _unPackedLength;
       _packedLength =  (int)Math.Ceiling(len / 2);
    }
    
    #region ILengthFormatter Members
    
    /// <summary>
    ///   Get the length of the packed length indicator.  For fixed length fields this is 0
    /// </summary>
    public int LengthOfLengthIndicator
    {
        get { return 0; }
    }
    
    /// <summary>
    ///   The maximum length of the field displayed as a string for descriptors
    /// </summary>
    public string MaxLength
    {
        get { return _unPackedLength.ToString(); }
    }
    
    /// <summary>
    ///   Descriptor for the length formatter used in ToString methods
    /// </summary>
    public string Description
    {
        get { return "FixedBcd"; }
    }
    
    /// <summary>
    ///   Get the length of the field
    /// </summary>
    /// <param name = "msg">Byte array of message data</param>
    /// <param name = "offset">offset to start parsing</param>
    /// <returns>The length of the field</returns>
    public int GetLengthOfField(byte[] msg, int offset)
    {
        return _unPackedLength;
    }
    
    /// <summary>
    ///   Pack the length header into the message
    /// </summary>
    /// <param name = "msg">Byte array of the message</param>
    /// <param name = "length">The length to pack into the message</param>
    /// <param name = "offset">Offset to start the packing</param>
    /// <returns>offset for the start of the field</returns>
    public int Pack(byte[] msg, int length, int offset)
    {
        return offset;
    }
    
    /// <summary>
    ///   Check the length of the field is valid
    /// </summary>
    /// <param name = "packedLength">the packed length of the field</param>
    /// <returns>true if valid, false otherwise</returns>
    public bool IsValidLength(int packedLength)
    {
        return packedLength == _packedLength;
    }
    
    #endregion
    

    }

  2. Modified the FieldDescription class / BcdFixed declaration

    /// /// The bcd fixed. /// /// /// The length. /// /// /// public static IFieldDescriptor BcdFixed(int unpackedLength) { return Create(new FixedLengthBcdFormatter(unpackedLength), FieldValidators.N, Formatters.Bcd, null); }

  3. Then change your formatter declaration to provide the unpacked length as the paramter.

    Bit._003_PROC_CODE, FieldDescriptor.BcdFixed(6)},

Again, all this may have been unnecessary because I didn't know something about the existing code, but it is working for me.

I hope this helps.