Welcome to the Tweaking4All community forums!
When participating, please keep the Forum Rules in mind!
Topics for particular software or systems: Start your topic link with the name of the application or system.
For example “MacOS X – Your question“, or “MS Word – Your Tip or Trick“.
Please note that switching to another language when reading a post will not bring you to the same post, in Dutch, as there is no translation for that post!
[Solved] Pascal - Converting large Hexadecimal string to long integer
Delphi, Lazarus, Free Pascal
(@hans)
Famed Member Admin
Joined: 12 years ago
Posts: 2859
Topic starter
June 6, 2014 4:14 AM
While writing ApplePi-Baker, I needed a function that can convert a large hexadecimal number (in a string) to a long integer (int64).
Since it took me a bit to create a working function, I figured others might benefit from the code as well, so here it is.
It takes a hexadecimal string and returns a int64, and it's a modified version of code I found on the Lazarus Pascal Forum.
function LongHexToDec(Str: string): Int64;
var
Counter : Integer;
DecValue: Int64;
begin
Result :=0;
DecValue:=1;
Str :=AnsiUpperCase(Str);
for Counter:=Length(Str) downto 1 do
begin
case Str[Counter] of
'1'..'9': Result:=Result+(Ord(Str[Counter])-Ord('0'))*DecValue;
'A'..'F': Result:=Result+(Ord(Str[Counter])-Ord('A')+10)*DecValue;
end;
DecValue:=DecValue shl 4;
end;
end;