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.
As mentioned in the question, I changed in VariableLengthFormatter.Pack(...):
to
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:
This fixes the BCD format.
For the Binary format, I added to static BinaryFormatter.GetBytes(...):
padding it to an even number of nibbles.
I also changed BinaryFormatter.GetPackedLength(...):
to:
rounding the formatted length up, instead of down.
And in FieldDescriptor.cs, after BinaryFixed(...), I added a BinaryVar(...) method:
That's it. Hope to get confirmations on the fix.