Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/runtime/src/types/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
33 changes: 33 additions & 0 deletions test/types/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading