Menu

#16 Error parsing binary message

1.5.7
open
nobody
None
1
2015-10-07
2015-10-07
No

When using Binary mode, there is an error when parsing numeric field length is odd number.
the error is here :

public static BigInteger decodeToBigInteger(byte[] buf, int pos, int length)
throws IndexOutOfBoundsException {
char[] digits = new char[length];
int start = 0;
for (int i = pos; i < pos + (length / 2) + (length % 2); i++) {
=> Should ignore the first digit because there is a padding with 0 to fill the hex value (20 digits instead of 19!)
digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);
digits[start++] = (char)((buf[i] & 0x0f) + 48);
}
return new BigInteger(new String(digits));
}
Try this with field 2 length = 19.
You should ignore the first zero when parsing odd binary value.

Feel free to contact me if you need any help on this.

Discussion

  • Alexandre Duquette

    I changed your code to :

    int i = pos;
    if (length % 2 != 0) {
    digits[start++] = (char)((buf[i] & 0x0f) + 48);
    i++;
    }

    for (; i < pos + (length / 2) + (length % 2); i++) {
    digits[start++] = (char)(((buf[i] & 0xf0) >> 4) + 48);
    digits[start++] = (char)((buf[i] & 0x0f) + 48);
    }

     
  • Enrique Zamudio Lopez

    I'll add tests for this. Please file an issue on github to keep track of it (I don't use SF anymore except for the project homepage)

     

Anonymous
Anonymous

Add attachments
Cancel