From d32134dd1a0b52525a3dbb6c5083b8d6e90647d2 Mon Sep 17 00:00:00 2001 From: Sven/Sarah Barth Date: Tue, 19 Apr 2022 14:21:26 +0200 Subject: [PATCH] * fix #39664: ensure that the 8- and 16-bit signed overloads of IntToHex return the correct number of digits + added test --- rtl/objpas/sysutils/sysstr.inc | 4 ++-- tests/webtbs/tw39664.pp | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 tests/webtbs/tw39664.pp diff --git a/rtl/objpas/sysutils/sysstr.inc b/rtl/objpas/sysutils/sysstr.inc index dfece586f1..4c101e3b22 100644 --- a/rtl/objpas/sysutils/sysstr.inc +++ b/rtl/objpas/sysutils/sysstr.inc @@ -919,7 +919,7 @@ end; function IntToHex(Value: Int8): string; begin - Result:=IntToHex(Value, 2*SizeOf(Int8)); + Result:=IntToHex(LongInt(Value) and $ff, 2*SizeOf(Int8)); end; function IntToHex(Value: UInt8): string; @@ -929,7 +929,7 @@ end; function IntToHex(Value: Int16): string; begin - Result:=IntToHex(Value, 2*SizeOf(Int16)); + Result:=IntToHex(LongInt(Value) and $ffff, 2*SizeOf(Int16)); end; function IntToHex(Value: UInt16): string; diff --git a/tests/webtbs/tw39664.pp b/tests/webtbs/tw39664.pp new file mode 100644 index 0000000000..aacde94caf --- /dev/null +++ b/tests/webtbs/tw39664.pp @@ -0,0 +1,43 @@ +program tw39664; + +{$mode Delphi} + +uses + SysUtils; + +var + i8: Int8; + i16: Int16; + i32: Int32; + u32: UInt32; + s: String; + +begin + i8 := -42; + s := IntToHex(i8); + writeln(s); + if s <> 'D6' then halt(1); + + i16 := -42; + s := IntToHex(i16); + writeln(s); + if s <> 'FFD6' then halt(2); + + i32 := -42; + s := IntToHex(i32); + writeln(s); + if s <> 'FFFFFFD6' then halt(3); + + s := i8.ToHexString; + writeln(s); + if s <> 'D6' then halt(4); + + s := i16.ToHexString; + writeln(s); + if s <> 'FFD6' then halt(5); + + s := i32.ToHexString; + writeln(s); + if s <> 'FFFFFFD6' then halt(6); +end. +