Welcome, Guest
Username: Password: Remember me
Discussions for CodeTyphon Object Pascal Programming Language
  • Page:
  • 1

TOPIC:

How to convert hex to integer? 9 years 1 month ago #8595

  • usbdoo
  • usbdoo's Avatar Topic Author
  • Visitor
  • Visitor
How to convert hex to integer?

This is lazarusu.
Hex2Dec
HexToInt
In CodeTyphon not find this
Hex2Dec
HexToInt

Please Log in or Create an account to join the conversation.

How to convert hex to integer? 9 years 1 month ago #8596

  • usbdoo
  • usbdoo's Avatar Topic Author
  • Visitor
  • Visitor
Ok found

uses strutils

Please Log in or Create an account to join the conversation.

How to convert hex to integer? 9 years 1 month ago #8597

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4538
  • Thank you received: 1116
use "strutils" unit Sir
it's part of FPC rtl-objpas package
function Hex2Dec(const S: string): Longint;

HexToInt don't exists in FPC 3.1.1 trunk

but you can find code samples at :
C:\codetyphon\typhon\components\pl_LNet\source\lhttp.pp line 546
C:\codetyphon\typhon\components\pl_SpkToolBar\source\SpkXMLParser.pas line 1316
C:\codetyphon\typhon\components\pl_DWScript\source\dwsTokenizer.pas line 584

PS: Please use Find files tool of CTCenter to find and text in files
double-click at file line will open CTCenter Editor to view the file
PilotLogic Architect and Core Programmer
Attachments:

Please Log in or Create an account to join the conversation.

Last edit: by Sternas Stefanos.

How to convert hex to integer? 9 years 1 month ago #8600

  • usbdoo
  • usbdoo's Avatar Topic Author
  • Visitor
  • Visitor
Thanks
unit strutils;
function Hex2Dec(const S: string): Longint;
var
HexStr: string;
begin
if Pos('$',S)=0 then
HexStr:='$'+ S
else
HexStr:=S;
Result:=StrToInt(HexStr);
end;



IntToStr(StrToInt64('$2E3B9352E02EB530')); /// this is ok 3331418333528372528
IntToStr(Hex2Dec('2E3B9352E02EB530'); /// this is wrong function Hex2Dec(const S: string): Longint;



Please fix this bug;

function Hex2Dec(const S: string): Int64;
var
HexStr: string;
begin
if Pos('$',S)=0 then
HexStr:='$'+ S
else
HexStr:=S;
Result:=StrToInt64(HexStr);
end;
IntToStr(Hex2Dec('2E3B9352E02EB530'); /// /// this is ok 3331418333528372528 function Hex2Dec(const S: string): Int64;

Please Log in or Create an account to join the conversation.

How to convert hex to integer? 9 years 1 month ago #8601

  • Sternas Stefanos
  • Sternas Stefanos's Avatar
  • Offline
  • Moderator
  • Moderator
  • Ex Pilot, M.Sc, Ph.D
  • Posts: 4538
  • Thank you received: 1116
Thanks Sir
we will try
PilotLogic Architect and Core Programmer

Please Log in or Create an account to join the conversation.

How to convert hex to integer? 9 years 1 month ago #8606

  • usbdoo
  • usbdoo's Avatar Topic Author
  • Visitor
  • Visitor
unit arConvertBase;

{$IFDEF FPC}
{$mode objfpc}{$H+}
{$ENDIF}

interface

function convertBase(AStr: string; fromBase, toBase: integer): string;
implementation

uses
  Classes, SysUtils, Math;

type
  TByteArray = array of byte;

function add(x, y: TByteArray; base: integer): TByteArray;
var
  n, carry, i, xi, yi, zi: integer;
begin
  n := Length(y); if Length(x) > Length(y) then n := Length(x);

  carry := 0;
  i := 0;
  SetLength(Result, 0);
  while (i < n) or (carry > 0) do begin
    xi := 0; if i < Length(x) then xi := x[i];
    yi := 0; if i < Length(y) then yi := y[i];

    zi := carry + xi + yi;

    SetLength(Result, Length(Result)+1);
    Result[High(Result)] := zi mod base;

    carry := floor(zi / base);
    Inc(i);
  end;

end;


function multiplyByNumber(num: integer; x: TByteArray; base: integer): TByteArray;
var
  power: TByteArray;
begin
  if num < 0 then exit;
  if num = 0 then exit;

  power := x;
  SetLength(Result, 0);

  while (true) do begin
    if (num and 1 > 0) then begin
      Result := add(Result, power, base);
    end;

    num := num shr 1;
    if num = 0 then break;
    power := add(power, power, base);
  end;
end;

function parseChar(const Value: Char; const Base: integer): Byte;
var
  tmpStr: string;
begin
  Result := 0;
  case Value of
    '0'..'9': Result := Ord(Value) - Ord('0');
    'A'..'Z',
    'a'..'z': begin
      tmpStr := UpperCase(Value);
      Result := Ord(tmpStr[1]) - Ord('A') + 10;
    end;
  end;
end;

function parseToDigitsArray(str: string; base: integer): TByteArray;
var i: integer;
begin
  //Result := nil;
  for i := 1 to Length(str) do begin
    SetLength(Result, Length(Result)+1);
    Result[High(Result)] := parseChar(str[Length(str)-i+1], base);
  end;
end;

function convertBase(AStr: string; fromBase, toBase: integer): string;
var digits: TByteArray;
  outArray, power: TByteArray;
  i: integer;
begin
  //outArray := nil;
  digits := parseToDigitsArray(AStr, fromBase);
  SetLength(power, 1); Power[High(power)] := 1;

  for i := 0 to Length(digits)-1 do begin
    if digits[i] > 0 then begin
      outArray := add(outArray, multiplyByNumber(digits[i], power, toBase), toBase);
    end;
    power := multiplyByNumber(fromBase, power, toBase);
  end;

  Result := '';
  for i := 1 to Length(outArray) do begin
    Result := Result + IntToStr(outArray[Length(outArray)-i]);
  end;
end;

end.


procedure TForm1.Button5Click(Sender: TObject);
begin
 showmessage(arconvertbase.convertBase('a7e5f55e1dbb48b799268e1a6d8618a3',16,10)); // result   223175087923687075112234402528973166755
 showmessage(arconvertbase.convertBase('2E3B9352E02EB530',16,10));                 // result   3331418333528372528
//  IntToStr(StrToInt64('$2E3B9352E02EB530'));                                        result   3331418333528372528
//  IntToStr(Hex2Dec('2E3B9352E02EB530');                                             result   3331418333528372528    function Hex2Dec(const S: string): Int64;
end;      

Please Log in or Create an account to join the conversation.

Last edit: by usbdoo.
  • Page:
  • 1