jpos / jPOS

jPOS Project
http://jpos.org
GNU Affero General Public License v3.0
599 stars 458 forks source link

issue #568: fixed BERTLVAsciiHexPackager getUninterpretLength always returns 0 with AsciiHex interpreter #570

Closed T-eli closed 7 months ago

T-eli commented 7 months ago

The issue was due to The two operands lengthAdjusted and interpreter.getPackedLength(lengthAdjusted) are both Integers.

the result was calculated using Integer arithmetic resulting in a 0,5 that was always rounded down to 0.

0.5 because AsciiHex packed len is double len

solution is to cast one of the operands to a double to use double arithmetic, then cast result to integer.

ar commented 7 months ago

Thank you for the PR and detailed Unit test.

There's no need for the double conversion, @barspi suggest a simpler solution, just

return (length * lengthAdjusted) / interpreter.getPackedLength(lengthAdjusted);

While you are at it, it would be great to also change the following method:

 private int getUninterpretLength(int length, Interpreter interpreter) {
        if (length > 0) {
            int lengthAdjusted = length + length % 2;
            return (length * lengthAdjusted) / interpreter.getPackedLength(lengthAdjusted);
        }
        return 0;
    }

(the one that uses an Interpreter instead of a BinaryInterpreter.

Would you like to send a new PR our of your branch or you prefer us to do it? Please confirm.

T-eli commented 7 months ago

Hello @ar

I've pushed a new commit with the suggested edits , you should be able to see it added to the PR.