Recent

Author Topic: [ SOLVED ] IntToHex on negative number  (Read 1665 times)

superc

  • Full Member
  • ***
  • Posts: 241
Re: [ SOLVED ] IntToHex on negative number
« Reply #15 on: August 11, 2022, 12:22:41 pm »
Same behavior, I am attaching the whole project

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: [ SOLVED ] IntToHex on negative number
« Reply #16 on: August 11, 2022, 12:27:21 pm »
Which proves my point: your executable returns the correct value FFEC .... Two byte format.
« Last Edit: August 11, 2022, 12:30:38 pm by Thaddy »
Specialize a type, not a var.

dseligo

  • Hero Member
  • *****
  • Posts: 1196
Re: [ SOLVED ] IntToHex on negative number
« Reply #17 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.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: [ SOLVED ] IntToHex on negative number
« Reply #18 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.    

« Last Edit: August 11, 2022, 01:10:01 pm by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Re: [ SOLVED ] IntToHex on negative number
« Reply #19 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.
« Last Edit: August 11, 2022, 01:15:02 pm by Thaddy »
Specialize a type, not a var.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: [ SOLVED ] IntToHex on negative number
« Reply #20 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.  
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: [ SOLVED ] IntToHex on negative number
« Reply #21 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.

Josh

  • Hero Member
  • *****
  • Posts: 1271
Re: [ SOLVED ] IntToHex on negative number
« Reply #22 on: August 11, 2022, 02:49:44 pm »
confirmed ok with latest fixes

The best way to get accurate information on the forum is to post something wrong and wait for corrections.

 

TinyPortal © 2005-2018