Recent

Author Topic: [SOLVED] Object Pascal: Converting Integer to Hexadecimal  (Read 579 times)

hymcode

  • New Member
  • *
  • Posts: 13
[SOLVED] Object Pascal: Converting Integer to Hexadecimal
« on: August 14, 2020, 12:23:42 pm »
Hello all. I am trying to make a GUI program that converts a number into it's hexademical form. I was able to do this sucessfully in a pascal TUI program I made a while back, but cannot seem to apply the same logic in Object Pascal.

In my TUI pascal program I was able to convert from Int to Hex like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   intNum, hexNum : Int64;
  3. begin
  4.   intNum := 12345;
  5.   hexNum := 0;
  6.  
  7.   hexNum := IntToHex(intNum, 4);
  8. end.
Code: Text  [Select][+][-]
  1. Output: 3039

In my Object Pascal GUI program I have 2 Edit boxes and a Button and upon clicking the Button I would like the number in Edit box 1 to be converted and display the hex form in Edit box 2.

Initially I tried this method to convert the String to an Integer:
Code: Pascal  [Select][+][-]
  1. var
  2.   num1, num2 : Integer;
  3. begin
  4.   num1 := StrToInt(Edit1.Text);
  5. end.

However with this method I wasn't able convert from Int to Hex using:
Code: Pascal  [Select][+][-]
  1. var
  2.   num1, num2 : Integer;
  3. begin
  4.   num2:= IntToHex(num1, 4);
  5. end.

This method unfortunately gave me a the following message which I don't get because I thought my earlier code converted the string into an Integer?
Code: Text  [Select][+][-]
  1. Error: Incompatible types: got "AnsiString" expected "LongInt"

My second attempt was to use a Turbo Pascal (new to me) method to convert from String to Integer, but got the same error message as above.
Code: Pascal  [Select][+][-]
  1. Val(Edit1.Text, num1, ErCode);
  2.   if ErCode <> 0 then
  3.     ShowMessage('Number cannot be converted');
  4.  
  5.   hexNum := IntToHex(num1, 4);

Number conversion is not a strong area of mine and any help would be greatly appreciated.
« Last Edit: August 25, 2020, 03:59:44 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

Handoko

  • Hero Member
  • *****
  • Posts: 3759
  • My goal: build my own game engine using Lazarus
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #1 on: August 14, 2020, 12:37:44 pm »
Maybe something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Num: Int64;
  4.   S:   string;
  5. begin
  6.   if not(TryStrToInt64(Edit1.Text, Num)) then Exit;
  7.   S := IntToHex(Num, 4);
  8.   ShowMessage(S);
  9. end;

wp

  • Hero Member
  • *****
  • Posts: 7537
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #2 on: August 14, 2020, 12:43:15 pm »
Initially I tried this method to convert the String to an Integer:
Code: Pascal  [Select][+][-]
  1. num1 := StrToInt(Edit1.Text);

However with this method I wasn't able convert from Int to Hex using:
Code: Pascal  [Select][+][-]
  1. num2:= IntToHex(num1, 4);

This method unfortunately gave me a the following message which I don't get because I thought my earlier code converted the string into an Integer?
Code: Text  [Select][+][-]
  1. Error: Incompatible types: got "AnsiString" expected "LongInt"

You do not show the declarations.

StrToInt() converts a string to an Integer, this means that num1 must be declared as an Integer.
IntToHex() converts an Integer to a string, this means than num2 must be declared as a String. Did you do it this way?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #3 on: August 14, 2020, 12:51:03 pm »
Maybe something like this:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   Num: Int64;
  4.   S:   string;
  5. begin
  6.   if not(TryStrToInt64(Edit1.Text, Num)) then Exit;
  7.   S := IntToHex(Num, 4);
  8.   ShowMessage(S);
  9. end;

Wow thank you very much  :). That worked perfectly. I've modified your code slightly and now I am able to get the converted number to display in my second Edit box.

Code: Pascal  [Select][+][-]
  1. if not (TryStrToInt64(Edit1.Text, Num)) then Exit;
  2.   S := IntToHex(Num, 4);
  3.   Edit2.Text:=S;
« Last Edit: August 14, 2020, 01:00:12 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #4 on: August 14, 2020, 12:57:10 pm »
Initially I tried this method to convert the String to an Integer:
Code: Pascal  [Select][+][-]
  1. num1 := StrToInt(Edit1.Text);

However with this method I wasn't able convert from Int to Hex using:
Code: Pascal  [Select][+][-]
  1. num2:= IntToHex(num1, 4);

This method unfortunately gave me a the following message which I don't get because I thought my earlier code converted the string into an Integer?
Code: Text  [Select][+][-]
  1. Error: Incompatible types: got "AnsiString" expected "LongInt"

You do not show the declarations.

StrToInt() converts a string to an Integer, this means that num1 must be declared as an Integer.
IntToHex() converts an Integer to a string, this means than num2 must be declared as a String. Did you do it this way?

Sorry about that. Yes you're right I forgot to show my declarations. I've updated my question now to clarify things.

Oh really IntToHex() actually converts the output to a String? I wasn't aware of this.
« Last Edit: August 14, 2020, 01:07:38 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

Handoko

  • Hero Member
  • *****
  • Posts: 3759
  • My goal: build my own game engine using Lazarus
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #5 on: August 14, 2020, 01:03:48 pm »

wp

  • Hero Member
  • *****
  • Posts: 7537
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #6 on: August 14, 2020, 01:05:02 pm »
I think you are not aware that the bit pattern of an integer does not depend on whether it is displayed as decimal or as hex. The difference is only in the string conversion.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #7 on: August 14, 2020, 01:08:40 pm »
I think you are not aware that the bit pattern of an integer does not depend on whether it is displayed as decimal or as hex. The difference is only in the string conversion.

Upon further inspection you are right. Thank you for this. I think I have a better understanding of the IntToHex() function now and was able to get the following code working:

Code: Pascal  [Select][+][-]
  1. var
  2.   num1 : Int64;
  3.   hexNum : String;
  4. begin
  5.   num1 := StrToInt64(Edit1.Text);
  6.   hexNum := IntToHex(num1, 4);
  7.   ShowMessage(hexNum);
  8. end.

Still so much more to learn :o
« Last Edit: August 14, 2020, 01:13:42 pm by hymcode »
Don’t wish it were easier; wish you were better. – Jim Rohn

hymcode

  • New Member
  • *
  • Posts: 13
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #8 on: August 14, 2020, 01:11:06 pm »
The documentation says so:
https://www.freepascal.org/docs-html/rtl/sysutils/inttohex.html

I recall looking at this when I made my TUI Int to Hex converter pascal program. I clearly brushed over the first line where it states "Convert an integer value to a hexadecimal string". Silly me indeed :-[
Don’t wish it were easier; wish you were better. – Jim Rohn

julkas

  • Guest

wp

  • Hero Member
  • *****
  • Posts: 7537
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #10 on: August 14, 2020, 01:30:46 pm »
Still so much more to learn :o
Good attitude (not only for you, but also for everybody)! Maybe one more thing: variable naming often leads to misunderstandings. When you name the result variable of the IntToHexfunction "hexNum" then I get the impression that it probably is a "number", maybe an integer or so. But if you name it "hexStr" then it is clear that you want it to be a string.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #11 on: August 14, 2020, 01:52:22 pm »
But if you name it "hexStr" then it is clear that you want it to be a string.
Uhm... did you take into account https://www.freepascal.org/docs-html/rtl/system/hexstr.html ?

Don't get me wrong, I still think it is a good suggestion but could work confusing. Not that I show it in the examples that I post here  but I prefer to use either a local/global prefix or a type prefix myself. for example in this particular case "sHexa" or "sHexStr". Also because we never know which (new) functions are added for each and every release (and/or what packages we use).

edit: ah, byte me. That was a silent hint, wasn't it wp ?  :-[
« Last Edit: August 14, 2020, 01:55:07 pm by TRon »

wp

  • Hero Member
  • *****
  • Posts: 7537
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #12 on: August 14, 2020, 03:44:57 pm »
edit: ah, byte me. That was a silent hint, wasn't it wp ?  :-[
What do you mean?
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

TRon

  • Hero Member
  • *****
  • Posts: 536
Re: Object Pascal Converting Integer to Hexadecimal
« Reply #13 on: August 14, 2020, 03:55:28 pm »
edit: ah, byte me. That was a silent hint, wasn't it wp ?  :-[
What do you mean?
I meant the suggestion you made to TS to use the name HexStr for the variable for a better understanding of the purpose/designation of the variable, but at the same time as a silent hint there also exists a function named HexStr ? e.g. would TS search for that specific name then TS would be able to discover that (easily).

Thaddy

  • Hero Member
  • *****
  • Posts: 10462
Re: [SOLVED] Object Pascal Converting Integer to Hexadecimal
« Reply #14 on: August 14, 2020, 05:06:36 pm »
One newer solution is missing:
Code: Pascal  [Select][+][-]
  1. var
  2.   intNum: Int64;
  3.   HexNum:String;
  4. begin
  5.   intNum := 12345;
  6.   hexNum := intNum.ToHexString;      // infers number of positions based on type
  7.   //or
  8.   hexNum := intNum.ToHexString(8);   // specified number of positions
  9. end.

Needs FPC 3.2.0 and works for all signed and unsigned types. Note: if required you have to add the leading $ yourself.
« Last Edit: August 14, 2020, 05:10:18 pm by Thaddy »
When you ask a question that is actually answered in the documentation, you are either lazy or a moron.

 

TinyPortal © 2005-2018