Cecil
19+ years progress programming and still learning.
I writing a Variable Length Encoding function based on some Java code. it looks quite simple but I get different results.
Here is the original Java Code:
Here is my ABL code equivalent:
When encoding 268435455 it should return "0xFF, 0xFF, 0xFF, 0x7F" but it return "0xFF,0x80,0x80,0x80,0x00" why would that be?
Here is the original Java Code:
Code:
public class VariableLengthEncoding {
public static Vector encodeValue(int value) {
Vector<Integer> digits = new Vector();
do {
int digit = value % 128;
value = value / 128;
// if there are more digits to encode, set the top bit of this digit
if ( value > 0 ) {
digit = digit | 0x80;
}
digits.add(digit);
}
while ( value > 0 );
return digits;
}
}
Here is my ABL code equivalent:
Code:
FUNCTION bitWiseOR RETURNS INTEGER (INPUT X AS INTEGER, INPUT Y AS INTEGER):
DEFINE VARIABLE b1 AS INTEGER NO-UNDO.
DEFINE VARIABLE b2 AS INTEGER NO-UNDO.
DEFINE VARIABLE n AS INTEGER NO-UNDO.
DEFINE VARIABLE Z AS INTEGER NO-UNDO.
DO n = 1 TO 32:
ASSIGN
b1 = GET-BITS(X, n, 1)
b2 = GET-BITS(Y, n, 1)
.
IF b1 = 1 OR b2 = 1 THEN PUT-BITS(Z, n, 1) = 1.
END.
RETURN Z.
END FUNCTION.
function variableLengthEncoding returns character(input x as integer):
define variable encodeByte as integer no-undo.
define variable encodeBytes as character no-undo.
define variable encodeByteRaw as raw no-undo.
do while x > 0:
encodeByte = x mod 128.
x = x / 128.
/** if there are more digits to encode, set the top bit of this digit **/
if (x > 0) then
put-byte(encodeByteRaw, 1) = bitWiseOR(input encodeByte, input 0x80).
encodeBytes = encodeBytes + ",0x" + caps(hex-encode(encodeByteRaw)).
end.
encodeBytes = left-trim(encodeBytes , ',').
return encodeBytes.
end.
message VariableLengthEncoding (INPUT 268435455).
When encoding 268435455 it should return "0xFF, 0xFF, 0xFF, 0x7F" but it return "0xFF,0x80,0x80,0x80,0x00" why would that be?
Last edited: