diff --git a/packages/runtime/src/types/integer.ts b/packages/runtime/src/types/integer.ts index 674e5884d..e304a3872 100644 --- a/packages/runtime/src/types/integer.ts +++ b/packages/runtime/src/types/integer.ts @@ -88,11 +88,11 @@ export class Integer implements INumeric { } else if (value instanceof Float || value instanceof DecFloat34) { this.set(Math.round(value.getRaw())); } else if (value instanceof Hex || value instanceof XString || value instanceof HexUInt8) { - let num = parseInt(value.get(), 16); -// handle two complement, - if ((value instanceof Hex || value instanceof HexUInt8) - && value.getLength() >= 4) { - const maxVal = Math.pow(2, value.get().length / 2 * 8); + const hex = value.get(); + let num = parseInt(hex, 16); +// handle two complement, the first bit is the sign + if (hex.length >= 8) { + const maxVal = Math.pow(2, hex.length / 2 * 8); if (num > maxVal / 2 - 1) { num = num - maxVal; } diff --git a/test/types/integer.ts b/test/types/integer.ts index 5fd92ecf9..e63075cea 100644 --- a/test/types/integer.ts +++ b/test/types/integer.ts @@ -173,6 +173,39 @@ describe("Running Examples - Integer type", () => { expect(abap.console.get()).to.equal("45141"); }); + it("hex to int, first bit is the sign", async () => { + const code = ` + DATA lv_hex TYPE x LENGTH 4 VALUE 'FFFFFFFF'. + DATA lv_int TYPE i. + lv_int = lv_hex. + ASSERT lv_int = -1.`; + const js = await run(code); + const f = new AsyncFunction("abap", js); + await f(abap); + }); + + it("hex offset to int, first bit is the sign", async () => { + const code = ` + DATA lv_hex TYPE x LENGTH 5 VALUE '00FFFFFFFF'. + DATA lv_int TYPE i. + lv_int = lv_hex+1. + ASSERT lv_int = -1.`; + const js = await run(code); + const f = new AsyncFunction("abap", js); + await f(abap); + }); + + it("hex offset length to int, positive", async () => { + const code = ` + DATA lv_hex TYPE x LENGTH 5 VALUE '00FFFFFFFF'. + DATA lv_int TYPE i. + lv_int = lv_hex+1(2). + ASSERT lv_int = 65535.`; + const js = await run(code); + const f = new AsyncFunction("abap", js); + await f(abap); + }); + it("preceding zeros", async () => { const code = ` DATA int TYPE i.