Recent

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

superc

  • Full Member
  • ***
  • Posts: 193
[ SOLVED ] IntToHex on negative number
« 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.

« Last Edit: August 11, 2022, 10:40:28 am by superc »

PascalDragon

  • Hero Member
  • *****
  • Posts: 4515
  • Compiler Developer
Re: IntToHex on negative number
« Reply #1 on: August 10, 2022, 01:36:37 pm »
This is a bug 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

y.ivanov

  • Sr. Member
  • ****
  • Posts: 497
Re: IntToHex on negative number
« Reply #2 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.
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.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 4515
  • Compiler Developer
Re: IntToHex on negative number
« Reply #3 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.

Please note that the digits parameter is the minimum number of digits (as mentioned in the documentation you linked).

y.ivanov

  • Sr. Member
  • ****
  • Posts: 497
Re: IntToHex on negative number
« Reply #4 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.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

superc

  • Full Member
  • ***
  • Posts: 193
Re: IntToHex on negative number
« Reply #5 on: August 10, 2022, 02:29:22 pm »
So I have to update the fpc to fix .... thanks a lot

y.ivanov

  • Sr. Member
  • ****
  • Posts: 497
Re: IntToHex on negative number
« Reply #6 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.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

BrunoK

  • Sr. Member
  • ****
  • Posts: 343
  • Retired programmer
Re: IntToHex on negative number
« Reply #7 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.

Bart

  • Hero Member
  • *****
  • Posts: 4744
    • Bart en Mariska's Webstek
Re: IntToHex on negative number
« Reply #8 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.

Bart

superc

  • Full Member
  • ***
  • Posts: 193
Re: IntToHex on negative number
« Reply #9 on: August 11, 2022, 10:40:12 am »
You are all very kind, thank you again

Thaddy

  • Hero Member
  • *****
  • Posts: 11889
Re: [ SOLVED ] IntToHex on negative number
« Reply #10 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...)
« Last Edit: August 11, 2022, 11:55:46 am by Thaddy »
Black themes should be banned.

superc

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

Thaddy

  • Hero Member
  • *****
  • Posts: 11889
Re: [ SOLVED ] IntToHex on negative number
« Reply #12 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.
« Last Edit: August 11, 2022, 12:02:04 pm by Thaddy »
Black themes should be banned.

superc

  • Full Member
  • ***
  • Posts: 193
Re: [ SOLVED ] IntToHex on negative number
« Reply #13 on: August 11, 2022, 12:02:08 pm »
Maybe I'm wrong something, but I don't understand what

Thaddy

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

« Last Edit: August 11, 2022, 12:23:08 pm by Thaddy »
Black themes should be banned.

 

TinyPortal © 2005-2018