Lazarus

Programming => General => Topic started by: superc on August 10, 2022, 01:00:56 pm

Title: [ SOLVED ] IntToHex on negative number
Post by: superc on August 10, 2022, 01:00:56 pm
Hello,

I writed this code for testing:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   x: Int16;
  4.   dc: string;
  5. begin  
  6.   x:= -20;
  7.   dc := IntToHex(x, 2);
  8.   ShowMessage(dc);
  9. end;
  10.  

printed result is 'FFFFFFEC',

I would have expected a result like 'FFEC',
where am I doing wrong?

Thanks in advance.

Title: Re: IntToHex on negative number
Post by: PascalDragon on August 10, 2022, 01:36:37 pm
This is a bug (https://gitlab.com/freepascal.org/fpc/source/-/commit/d32134dd1a0b52525a3dbb6c5083b8d6e90647d2) that was fixed in April. As a workaround you need to call it like this:

Code: Pascal  [Select][+][-]
  1. IntToHex(LongInt(x) and $ffff, 2); // though for Int16 you should pass 4, not 2
Title: Re: IntToHex on negative number
Post by: alpine on August 10, 2022, 01:39:33 pm
Hello,

I writed this code for testing:
Code: Pascal  [Select][+][-]
  1.  
  2. var
  3.   x: Int16;
  4.   dc: string;
  5. begin  
  6.   x:= -20;
  7.   dc := IntToHex(x, 2);
  8.   ShowMessage(dc);
  9. end;
  10.  

printed result is 'FFFFFFEC',

I would have expected a result like 'FFEC',
where am I doing wrong?

Thanks in advance.
A negative number is a large unsigned number which doesn't fit in the number of digits you specified (2). See Description (https://www.freepascal.org/docs-html/rtl/sysutils/inttohex.html).
There is no overload for Int16 with 2 params, the 1-st argument is sign-extended to LongInt first, then the function:
Code: Pascal  [Select][+][-]
  1. IntToHex(
  2.   Value: LongInt;
  3.   Digits: Integer
  4. ):string;
is used without truncation.
Title: Re: IntToHex on negative number
Post by: PascalDragon on August 10, 2022, 01:56:37 pm
A negative number is a large unsigned number which doesn't fit in the number of digits you specified (2). See Description (https://www.freepascal.org/docs-html/rtl/sysutils/inttohex.html).

Please note that the digits parameter is the minimum number of digits (as mentioned in the documentation you linked).
Title: Re: IntToHex on negative number
Post by: alpine on August 10, 2022, 02:04:34 pm
Please note that the digits parameter is the minimum number of digits (as mentioned in the documentation you linked).
Exactly what I've meant with the link and:
Quote
is used without truncation.
Title: Re: IntToHex on negative number
Post by: superc on August 10, 2022, 02:29:22 pm
So I have to update the fpc to fix .... thanks a lot
Title: Re: IntToHex on negative number
Post by: alpine on August 10, 2022, 03:04:44 pm
So I have to update the fpc to fix .... thanks a lot
No. It won't help for that snippet. Read the docs more carefully and change your code accordingly.
Title: Re: IntToHex on negative number
Post by: BrunoK on August 10, 2022, 04:17:43 pm
Code: Pascal  [Select][+][-]
  1. function UIntToHex(Value: UInt32; Digits: integer): string;
  2. const
  3.   HexDigits: array[0..15] of char = '0123456789ABCDEF';
  4. var
  5.   i: integer;
  6. begin
  7.   if Digits = 0 then
  8.     Digits := 1;
  9.   SetLength(Result, digits);
  10.   for i := 0 to digits - 1 do begin
  11.     Result[digits - i] := HexDigits[Value and 15];
  12.     Value := Value shr 4;
  13.   end;
  14. end;
  15.  
  16. var
  17.   x16: Int16;
  18.   x32: Int32;
  19.   xInt: Int32;
  20.   dc: string;
  21. begin
  22.   x16:= -20;
  23.   dc := UIntToHex(x16, SizeOf(x16)*2);
  24.   WriteLn(dc);
  25.   x32:= -20;
  26.   dc := UIntToHex(x16, SizeOf(x32)*2);
  27.   WriteLn(dc);
  28.   xInt:= -20;
  29.   dc := UIntToHex(xInt, SizeOf(xInt)*2);
  30.   WriteLn(dc);
  31.   WriteLn(dc);
  32.   xInt:= 20;
  33.   dc := UIntToHex(xInt, SizeOf(xInt)*2);
  34.   WriteLn(dc);
  35.   ReadLn;
  36. end.
Title: Re: IntToHex on negative number
Post by: Bart on August 10, 2022, 06:08:58 pm
I have a IntToStrBase() function that will return something like '-F' if the input is -15 (and base = 16).
You can find it here (http://svn.code.sf.net/p/flyingsheep/code/trunk/MijnLib/fsiconv.pp).

Bart
Title: Re: IntToHex on negative number
Post by: superc on August 11, 2022, 10:40:12 am
You are all very kind, thank you again
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Thaddy on August 11, 2022, 10:52:34 am
Why so complicated? It is really easy (full program):
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. var
  3.   x: smallint; // use the proper Pascal syntax, int16 is an alias.
  4. begin  
  5.   x:= -20;
  6.   writeln(x.ToHexString); // make use of the type helpers
  7.   readln;
  8. end.
 
Note 3.2.X or higher is required.

Output is:
Code: Bash  [Select][+][-]
  1. FFEC

Overcomplicating things..........
(Note that I am responsible for the oversimplication that does ToHexString, so you can blame me...)
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: superc on August 11, 2022, 11:56:54 am
Why so complicated? It is really easy (full program):
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. var
  3.   x: smallint; // use the proper Pascal syntax, int16 is an alias.
  4. begin  
  5.   x:= -20;
  6.   writeln(x.ToHexString); // make use of the type helpers
  7.   readln;
  8. end.
 
Note 3.2.X or higher is required.

Output is:
Code: Bash  [Select][+][-]
  1. FFEC

Overcomplicating things..........
(Note that I am responsible for the oversimplication)

I'm programming with fpc 3.2.3 for 386 Win32 and doesn't work, I obtain FFFFFFCE for result ....
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Thaddy on August 11, 2022, 11:58:36 am
Did you actually compile my example? Because it works on any platform that I use.
Note the notes.

Show me the code you tried, because my example is correct.
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: superc on August 11, 2022, 12:02:08 pm
Maybe I'm wrong something, but I don't understand what
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Thaddy on August 11, 2022, 12:15:57 pm
I get the correct output on win32/64 and Linux (AARCH, INTEL 32/64, ARM32) .
So are you sure there isn't another project1 in your path? Try renaming my code to something different and recompile.

Title: Re: [ SOLVED ] IntToHex on negative number
Post by: superc on August 11, 2022, 12:22:41 pm
Same behavior, I am attaching the whole project
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Thaddy on August 11, 2022, 12:27:21 pm
Which proves my point: your executable returns the correct value FFEC .... Two byte format.
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: dseligo on August 11, 2022, 12:36:35 pm
Why so complicated? It is really easy (full program):
Code: Pascal  [Select][+][-]
  1. uses sysutils;
  2. var
  3.   x: smallint; // use the proper Pascal syntax, int16 is an alias.
  4. begin  
  5.   x:= -20;
  6.   writeln(x.ToHexString); // make use of the type helpers
  7.   readln;
  8. end.
 
Note 3.2.X or higher is required.

Output is:
Code: Bash  [Select][+][-]
  1. FFEC

Overcomplicating things..........
(Note that I am responsible for the oversimplication that does ToHexString, so you can blame me...)

I tried under Debian, Lazarus 2.2.2, FPC 3.2.2; Windows 11, Lazarus 2.2.2, FPC 3.2.2 and Windows 11 FPC 3.2.0 (Free Pascal IDE) and everywhere is the same: FFFFFFEC. So there is something different in your setups.
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Josh on August 11, 2022, 01:07:08 pm
tried 3.2.3 fails

tried 3.3.1 ok

Code: Pascal  [Select][+][-]
  1. program Project1;
  2. uses sysutils;
  3. var
  4.   x: smallint; // use the proper Pascal syntax, int16 is an alias.
  5. begin
  6.   x:= -20;
  7.   writeln(x.ToHexString); // make use of the type helpers
  8.   readln;
  9. end.    

Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Thaddy on August 11, 2022, 01:10:18 pm
That is strange because it was merged into 3.2.0. Point me to my mistake?
Note that Avra did some work on the helpers later, so maybe we should ask Avra if he changed my code unintentionally?
(Can't see it, but possible) Strange that it works on my platforms.
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Josh on August 11, 2022, 01:26:10 pm
hi thaddy
something changed, relevent types does not have the type casting or relevent and $ff $ffff, effects int8,int16..
3.2.3 definitions
Code: Pascal  [Select][+][-]
  1. function IntToHex(Value: QWord; Digits: integer): string;
  2. begin
  3.   result:=IntToHex(Int64(Value),Digits);
  4. end;
  5.  
  6. function IntToHex(Value: Int8): string;
  7. begin
  8.   Result:=IntToHex(Value, 2*SizeOf(Int8));
  9. end;
  10.  
  11. function IntToHex(Value: UInt8): string;
  12. begin
  13.   Result:=IntToHex(Value, 2*SizeOf(UInt8));
  14. end;
  15.  
  16. function IntToHex(Value: Int16): string;
  17. begin
  18.   Result:=IntToHex(Value, 2*SizeOf(Int16));
  19. end;
  20.  
  21. function IntToHex(Value: UInt16): string;
  22. begin
  23.   Result:=IntToHex(Value, 2*SizeOf(UInt16));
  24. end;
  25.  
  26. function IntToHex(Value: Int32): string;
  27. begin
  28.   Result:=IntToHex(Value, 2*SizeOf(Int32));
  29. end;
  30.  
  31. function IntToHex(Value: UInt32): string;
  32. begin
  33.   Result:=IntToHex(Value, 2*SizeOf(UInt32));
  34. end;
  35.  
  36. function IntToHex(Value: Int64): string;
  37. begin
  38.   Result:=IntToHex(Value, 2*SizeOf(Int64));
  39. end;
  40.  
  41. function IntToHex(Value: UInt64): string;
  42. begin
  43.   Result:=IntToHex(Value, 2*SizeOf(UInt64));
  44. end;                
  45.  


3.3.1 definitions
Code: Pascal  [Select][+][-]
  1. function IntToHex(Value: QWord; Digits: integer): string;
  2. begin
  3.   result:=IntToHex(Int64(Value),Digits);
  4. end;
  5.  
  6. function IntToHex(Value: Int8): string;
  7. begin
  8.   Result:=IntToHex(LongInt(Value) and $ff, 2*SizeOf(Int8));
  9. end;
  10.  
  11. function IntToHex(Value: UInt8): string;
  12. begin
  13.   Result:=IntToHex(Value, 2*SizeOf(UInt8));
  14. end;
  15.  
  16. function IntToHex(Value: Int16): string;
  17. begin
  18.   Result:=IntToHex(LongInt(Value) and $ffff, 2*SizeOf(Int16));
  19. end;
  20.  
  21. function IntToHex(Value: UInt16): string;
  22. begin
  23.   Result:=IntToHex(Value, 2*SizeOf(UInt16));
  24. end;
  25.  
  26. function IntToHex(Value: Int32): string;
  27. begin
  28.   Result:=IntToHex(Value, 2*SizeOf(Int32));
  29. end;
  30.  
  31. function IntToHex(Value: UInt32): string;
  32. begin
  33.   Result:=IntToHex(LongInt(Value), 2*SizeOf(UInt32));
  34. end;
  35.  
  36. function IntToHex(Value: Int64): string;
  37. begin
  38.   Result:=IntToHex(Value, 2*SizeOf(Int64));
  39. end;
  40.  
  41. function IntToHex(Value: UInt64): string;
  42. begin
  43.   Result:=IntToHex(Value, 2*SizeOf(UInt64));
  44. end;                      
  45.  
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: PascalDragon on August 11, 2022, 01:38:47 pm
FPC 3.2.2 definitely does have erratic behaviour for IntToHex with a 8- or 16-Bit signed argument (and same for type helpers for 8 and 16-Bit signed types). I've only merged the corresponding fix from main to 3.2.3 yesterday.
Title: Re: [ SOLVED ] IntToHex on negative number
Post by: Josh on August 11, 2022, 02:49:44 pm
confirmed ok with latest fixes

TinyPortal © 2005-2018