View Issue Details

IDProjectCategoryView StatusLast Update
0037043FPCRTLpublic2020-05-16 22:48
ReporterBi0T1N Assigned ToSven Barth  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037043: Add missing IntToHex overloads
DescriptionFollowing IntToHex() overloads (http://docwiki.embarcadero.com/Libraries/Rio/en/System.SysUtils.IntToHex) are missing:
function IntToHex(Value: Int8): string;
function IntToHex(Value: UInt8): string;
function IntToHex(Value: Int16): string;
function IntToHex(Value: UInt16): string;
function IntToHex(Value: Int32): string;
function IntToHex(Value: UInt32): string;
function IntToHex(Value: Int64): string;
function IntToHex(Value: UInt64): string;
Additional InformationThe type helpers already provide a ToHexString function which also does what the missing functions do, I just don't know if the ToHexString should call the appropriate IntToHex function or if the new IntToHex functions should use Value.ToHexString to avoid code duplications.
TagsNo tags attached.
Fixed in Revision45370
FPCOldBugId
FPCTarget-
Attached Files

Activities

Bi0T1N

2020-05-09 22:17

reporter  

IntToHex_tests.pas (515 bytes)   
program test;

{$mode Delphi}

uses
  SysUtils;

var
  i8: Int8;
  u8: UInt8;
  i16: Int16;
  u32: UInt32;
  u64: Uint64;
  i: Integer;

begin
  i8 := 15;
  writeln(IntToHex(i8));

  u8 := 224;
  writeln(IntToHex(u8));

  i16 := 224;
  writeln(IntToHex(i16));

  u32 := 224;
  writeln(IntToHex(u32));

  u64 := 224;
  writeln(IntToHex(u64));

  i := 224;
  writeln(IntToHex(i));

  writeln(i8.ToHexString);
  writeln(u64.ToHexString);
  writeln(i.ToHexString);

  readln;
end.
IntToHex_tests.pas (515 bytes)   

Bi0T1N

2020-05-09 22:20

reporter   ~0122685

Attached a patch which adds the missing overloads without using/touching the type helpers (didn't know that ToHexString was already implemented).
01-Add_missing_IntToHex_overloads.patch (2,148 bytes)   
diff --git rtl/objpas/sysutils/sysstr.inc rtl/objpas/sysutils/sysstr.inc
index 529ac2a77c..6c53c7e03d 100644
--- rtl/objpas/sysutils/sysstr.inc
+++ rtl/objpas/sysutils/sysstr.inc
@@ -919,6 +919,46 @@ begin
   result:=IntToHex(Int64(Value),Digits);
 end;
 
+function IntToHex(Value: Int8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int8));
+end;
+
+function IntToHex(Value: UInt8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt8));
+end;
+
+function IntToHex(Value: Int16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int16));
+end;
+
+function IntToHex(Value: UInt16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt16));
+end;
+
+function IntToHex(Value: Int32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int32));
+end;
+
+function IntToHex(Value: UInt32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt32));
+end;
+
+function IntToHex(Value: Int64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int64));
+end;
+
+function IntToHex(Value: UInt64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt64));
+end;
+
 function TryStrToInt(const s: string; out i : Longint) : boolean;
 var Error : word;
 begin
diff --git rtl/objpas/sysutils/sysstrh.inc rtl/objpas/sysutils/sysstrh.inc
index 30d8716623..67b2a764ad 100644
--- rtl/objpas/sysutils/sysstrh.inc
+++ rtl/objpas/sysutils/sysstrh.inc
@@ -120,6 +120,14 @@ function UIntToStr(Value: Cardinal): string; {$ifdef SYSUTILSINLINE}inline;{$END
 function IntToHex(Value: Longint; Digits: integer): string;
 function IntToHex(Value: Int64; Digits: integer): string;
 function IntToHex(Value: QWord; Digits: integer): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int8): string;
+function IntToHex(Value: UInt8): string;
+function IntToHex(Value: Int16): string;
+function IntToHex(Value: UInt16): string;
+function IntToHex(Value: Int32): string;
+function IntToHex(Value: UInt32): string;
+function IntToHex(Value: Int64): string;
+function IntToHex(Value: UInt64): string;
 function StrToInt(const s: string): Longint;
 function StrToDWord(const s: string): DWord;
 function StrToInt64(const s: string): int64;

Sven Barth

2020-05-09 23:50

manager   ~0122690

Would you please provide a test that uses Halt() if any result does not match like you did for TBitConverter?

You could also declare your IntToHex overloads as "inline" (just like the IntToHex overload for QWord with the check for SYSUTILSINLINE).

Bi0T1N

2020-05-10 16:43

reporter   ~0122698

Adjusted test attached and also added the inline stuff (thought it shouldn't be done any more because the compiler should handle it?)
IntToHex_tests-2.pas (954 bytes)   
program test;

{$mode Delphi}

uses
  SysUtils;

var
  i8: Int8;
  u8: UInt8;
  i16: Int16;
  u32: UInt32;
  u64: Uint64;
  i: Integer;
  s: AnsiString;

begin
  i8 := 15;
  s := IntToHex(i8);
  writeln(s);
  if s <> '0F' then halt(1);

  u8 := 224;
  s := IntToHex(u8);
  writeln(s);
  if s <> 'E0' then halt(2);

  i16 := 224;
  s := IntToHex(i16);
  writeln(s);
  if s <> '00E0' then halt(3);

  u32 := 224;
  s := IntToHex(u32);
  writeln(s);
  if s <> '000000E0' then halt(4);

  u64 := 224;
  s := IntToHex(u64);
  writeln(s);
  if s <> '00000000000000E0' then halt(5);

  i := 224;
  s := IntToHex(i);
  writeln(s);
  if s <> '000000E0' then halt(6);

  s := i8.ToHexString;
  writeln(s);
  if s <> '0F' then halt(7);

  s := u64.ToHexString;
  writeln(s);
  if s <> '00000000000000E0' then halt(8);

  s := i.ToHexString;
  writeln(s);
  if s <> '000000E0' then halt(9);

  readln;
end.
IntToHex_tests-2.pas (954 bytes)   
02-Add_missing_IntToHex_overloads.patch (2,460 bytes)   
diff --git rtl/objpas/sysutils/sysstr.inc rtl/objpas/sysutils/sysstr.inc
index 529ac2a77c..6c53c7e03d 100644
--- rtl/objpas/sysutils/sysstr.inc
+++ rtl/objpas/sysutils/sysstr.inc
@@ -919,6 +919,46 @@ begin
   result:=IntToHex(Int64(Value),Digits);
 end;
 
+function IntToHex(Value: Int8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int8));
+end;
+
+function IntToHex(Value: UInt8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt8));
+end;
+
+function IntToHex(Value: Int16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int16));
+end;
+
+function IntToHex(Value: UInt16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt16));
+end;
+
+function IntToHex(Value: Int32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int32));
+end;
+
+function IntToHex(Value: UInt32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt32));
+end;
+
+function IntToHex(Value: Int64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int64));
+end;
+
+function IntToHex(Value: UInt64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt64));
+end;
+
 function TryStrToInt(const s: string; out i : Longint) : boolean;
 var Error : word;
 begin
diff --git rtl/objpas/sysutils/sysstrh.inc rtl/objpas/sysutils/sysstrh.inc
index 30d8716623..37ea687489 100644
--- rtl/objpas/sysutils/sysstrh.inc
+++ rtl/objpas/sysutils/sysstrh.inc
@@ -120,6 +120,14 @@ function UIntToStr(Value: Cardinal): string; {$ifdef SYSUTILSINLINE}inline;{$END
 function IntToHex(Value: Longint; Digits: integer): string;
 function IntToHex(Value: Int64; Digits: integer): string;
 function IntToHex(Value: QWord; Digits: integer): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int8): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt8): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int16): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt16): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int32): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt32): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int64): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt64): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
 function StrToInt(const s: string): Longint;
 function StrToDWord(const s: string): DWord;
 function StrToInt64(const s: string): int64;

Bi0T1N

2020-05-10 16:44

reporter   ~0122699

However, it's still open if the type helpers should call the new IntToHex overloads or if a different approach should be used.

Sven Barth

2020-05-10 22:34

manager   ~0122705

Thanks for the test case.

The AutoInline optimization isn't enabled by default (it's not even part of any optimization level, but is standalone) thus for now I prefer to use the inline directive.

Hmm... considering that we're going to rely on inlining you can adjust the type helpers as well. Should result in the same code anyway.

Bi0T1N

2020-05-11 14:43

reporter   ~0122713

I've enhanced the test so that it tests IntToHex and ToHexString for the used types and in the attached patch the type helpers use the new overload now.
IntToHex_tests-3.pas (1,180 bytes)   
program test;

{$mode Delphi}

uses
  SysUtils;

var
  i8: Int8;
  u8: UInt8;
  i16: Int16;
  u32: UInt32;
  u64: Uint64;
  i: Integer;
  s: AnsiString;

begin
  i8 := 15;
  s := IntToHex(i8);
  writeln(s);
  if s <> '0F' then halt(1);

  u8 := 224;
  s := IntToHex(u8);
  writeln(s);
  if s <> 'E0' then halt(2);

  i16 := 224;
  s := IntToHex(i16);
  writeln(s);
  if s <> '00E0' then halt(3);

  u32 := 224;
  s := IntToHex(u32);
  writeln(s);
  if s <> '000000E0' then halt(4);

  u64 := 224;
  s := IntToHex(u64);
  writeln(s);
  if s <> '00000000000000E0' then halt(5);

  i := 224;
  s := IntToHex(i);
  writeln(s);
  if s <> '000000E0' then halt(6);

  s := i8.ToHexString;
  writeln(s);
  if s <> '0F' then halt(7);

  s := u8.ToHexString;
  writeln(s);
  if s <> 'E0' then halt(8);

  s := i16.ToHexString;
  writeln(s);
  if s <> '00E0' then halt(9);

  s := u32.ToHexString;
  writeln(s);
  if s <> '000000E0' then halt(10);

  s := u64.ToHexString;
  writeln(s);
  if s <> '00000000000000E0' then halt(11);

  s := i.ToHexString;
  writeln(s);
  if s <> '000000E0' then halt(12);

  readln;
end.
IntToHex_tests-3.pas (1,180 bytes)   
03-Add_missing_IntToHex_overloads.patch (2,879 bytes)   
diff --git rtl/objpas/sysutils/syshelpo.inc rtl/objpas/sysutils/syshelpo.inc
index 27d938e3af..0ab24ea753 100644
--- rtl/objpas/sysutils/syshelpo.inc
+++ rtl/objpas/sysutils/syshelpo.inc
@@ -61,7 +61,7 @@ end;
 Function TORDINALHELPER.ToHexString: string; overload; inline;
 
 begin
-  Result:=IntToHex(Self,SizeOf(TORDINALTYPE)*2);
+  Result:=IntToHex(Self);
 end;
 
 Function TORDINALHELPER.ToSingle: Single; inline;
diff --git rtl/objpas/sysutils/sysstr.inc rtl/objpas/sysutils/sysstr.inc
index 529ac2a77c..6c53c7e03d 100644
--- rtl/objpas/sysutils/sysstr.inc
+++ rtl/objpas/sysutils/sysstr.inc
@@ -919,6 +919,46 @@ begin
   result:=IntToHex(Int64(Value),Digits);
 end;
 
+function IntToHex(Value: Int8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int8));
+end;
+
+function IntToHex(Value: UInt8): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt8));
+end;
+
+function IntToHex(Value: Int16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int16));
+end;
+
+function IntToHex(Value: UInt16): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt16));
+end;
+
+function IntToHex(Value: Int32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int32));
+end;
+
+function IntToHex(Value: UInt32): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt32));
+end;
+
+function IntToHex(Value: Int64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(Int64));
+end;
+
+function IntToHex(Value: UInt64): string;
+begin
+  Result:=IntToHex(Value, 2*SizeOf(UInt64));
+end;
+
 function TryStrToInt(const s: string; out i : Longint) : boolean;
 var Error : word;
 begin
diff --git rtl/objpas/sysutils/sysstrh.inc rtl/objpas/sysutils/sysstrh.inc
index 30d8716623..37ea687489 100644
--- rtl/objpas/sysutils/sysstrh.inc
+++ rtl/objpas/sysutils/sysstrh.inc
@@ -120,6 +120,14 @@ function UIntToStr(Value: Cardinal): string; {$ifdef SYSUTILSINLINE}inline;{$END
 function IntToHex(Value: Longint; Digits: integer): string;
 function IntToHex(Value: Int64; Digits: integer): string;
 function IntToHex(Value: QWord; Digits: integer): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int8): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt8): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int16): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt16): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int32): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt32): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: Int64): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
+function IntToHex(Value: UInt64): string; {$ifdef SYSUTILSINLINE}inline;{$ENDIF}
 function StrToInt(const s: string): Longint;
 function StrToDWord(const s: string): DWord;
 function StrToInt64(const s: string): int64;

Sven Barth

2020-05-15 17:14

manager   ~0122821

Thank you for the patch.

Please test and close if okay.

Issue History

Date Modified Username Field Change
2020-05-09 22:17 Bi0T1N New Issue
2020-05-09 22:17 Bi0T1N File Added: IntToHex_tests.pas
2020-05-09 22:20 Bi0T1N Note Added: 0122685
2020-05-09 22:20 Bi0T1N File Added: 01-Add_missing_IntToHex_overloads.patch
2020-05-09 23:50 Sven Barth Note Added: 0122690
2020-05-10 16:43 Bi0T1N Note Added: 0122698
2020-05-10 16:43 Bi0T1N File Added: IntToHex_tests-2.pas
2020-05-10 16:43 Bi0T1N File Added: 02-Add_missing_IntToHex_overloads.patch
2020-05-10 16:44 Bi0T1N Note Added: 0122699
2020-05-10 22:34 Sven Barth Note Added: 0122705
2020-05-11 14:43 Bi0T1N Note Added: 0122713
2020-05-11 14:43 Bi0T1N File Added: IntToHex_tests-3.pas
2020-05-11 14:43 Bi0T1N File Added: 03-Add_missing_IntToHex_overloads.patch
2020-05-15 15:30 Sven Barth Assigned To => Sven Barth
2020-05-15 15:30 Sven Barth Status new => assigned
2020-05-15 17:14 Sven Barth Status assigned => resolved
2020-05-15 17:14 Sven Barth Resolution open => fixed
2020-05-15 17:14 Sven Barth Fixed in Version => 3.3.1
2020-05-15 17:14 Sven Barth Fixed in Revision => 45370
2020-05-15 17:14 Sven Barth FPCTarget => -
2020-05-15 17:14 Sven Barth Note Added: 0122821
2020-05-16 22:48 Bi0T1N Status resolved => closed