1

I'm formatting a BCD field:

  msg[60] = FieldDescriptor.BcdVar(3, 125, Formatters.Bcd);

The lib throws an exception when packing the message. It's straight-forward to reproduce.

I found that in both BCD and Binary Var, when the length-indicator is odd (1 or 3), the exception occurs.

I changed in VariableLengthFormatter.Pack():

  var lengthStr = length.ToString().PadLeft(LengthOfLengthIndicator, '0');

to

  var lengthStr = length.ToString().PadLeft(_lengthIndicator, '0');

using the unpacked length for padding the string, and the issue was fixed (well, I added FieldDescriptor.BinaryVar() and a few fixes in BinaryFormatter, which I'll be glad to share).

So, my question is: was it a bug and was fixed, or am I miss-using the (nicely written) lib and barking the wrong tree?

If it is a bug - can it be fixed in some object-oriented magic in my code (like extending the class Iso8583 when wanting to change the default template format), or the fix must be in the lib itself and when a new lib version goes out there will be merge issues?

PS - I'm new to C# (experienced C programmer)

Thanks.

Rami Rosenbaum
  • 467
  • 5
  • 18
  • I wanted to propose that you create an [extension method](http://msdn.microsoft.com/en-us/library/vstudio/bb383977.aspx), but now I see that `FieldDescriptor.BcdVar` is a `static` method and you cannot extend that class with new static methods. So, yes, the best way is to patch the code and share the fixes with the community. :) – vgru Nov 22 '13 at 08:05

1 Answers1

1

As mentioned in the question, I changed in VariableLengthFormatter.Pack(...):

  var lengthStr = length.ToString().PadLeft(LengthOfLengthIndicator, '0');

to

  var lengthStr = length.ToString().PadLeft(_lengthIndicator, '0');

using the unpacked length for padding the Length-Indicator string with zeros.

The next line formats the now-corrected Length-Indicator according to the specified formatter:

  var header = _lengthFormatter.GetBytes(lengthStr);

This fixes the BCD format.

For the Binary format, I added to static BinaryFormatter.GetBytes(...):

  if (value.Length % 2 == 1)
    value = value.PadLeft(value.Length + 1, '0');

padding it to an even number of nibbles.

I also changed BinaryFormatter.GetPackedLength(...):

  return unpackedLength/2;

to:

  return (unpackedLength + 1) / 2;

rounding the formatted length up, instead of down.

And in FieldDescriptor.cs, after BinaryFixed(...), I added a BinaryVar(...) method:

  public static IFieldDescriptor BinaryVar(int lengthIndicator, int maxLength, IFormatter lengthFormatter)
  {
      return Create(new VariableLengthFormatter(lengthIndicator, maxLength, lengthFormatter), FieldValidators.Hex, Formatters.Binary, null);
  }

That's it. Hope to get confirmations on the fix.

Rami Rosenbaum
  • 467
  • 5
  • 18