Delphi Tokyo - Function: String to Hex and Hex to String

13,004

There are a couple of problems with your code:

  1. You are type-casting your input AnsiString incorrectly to PWideChar, so you are calling the wrong overload of HexToBin(). PWideChar should be PAnsiChar instead.

  2. The BufSize parameter of HexToBin() specifies the number of bytes the output buffer expects to receive, but you are passing it the number of characters in the hex string instead.

Also, since String2Hex() takes an AnsiString and returns a UnicodeString, Hex2String() should take UnicodeString and return an AnsiString to match.

Try this instead:

function String2Hex(const Buffer: AnsiString): string;
begin
  SetLength(Result, Length(Buffer) * 2);
  BinToHex(PAnsiChar(Buffer), PChar(Result), Length(Buffer));
end;

function Hex2String(const Buffer: string): AnsiString;
begin
  SetLength(Result, Length(Buffer) div 2);
  HexToBin(PChar(Buffer), PAnsiChar(Result), Length(Result));
end;

var
  hex: string;
  str: AnsiString;
begin
  hex := String2Hex('stackoverflow');
  ShowMessage(hex); // shows '737461636B6F766572666C6F77'
  str := Hex2String(hex);
  ShowMessage(str); // shows 'stackoverflow'
end;
Share:
13,004
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    The function below works perfectly to convert string to hexadecimal:

    function String2Hex(const Buffer: AnsiString): string;
    begin
      SetLength(Result, Length(Buffer) * 2);
      BinToHex(@Buffer[1], PWideChar(@Result[1]), Length(Buffer));
    end;
    
    ShowMessage(String2Hex('stackoverflow'));
    

    This result: "737461636B6F766572666C6F77"

    The problem is in the function of converting hexadecimal to string:

    function Hex2String(const Buffer: AnsiString): string;
    begin
      SetLength(Result, Length(Buffer) div 2);
      HexToBin(PWideChar(@Buffer[1]), @Result[1], Length(Buffer));
    end;
    
    ShowMessage(Hex2String('737461636B6F766572666C6F77'));
    

    The result should be "stackoverflow" but nothing happens.

    Could someone help me?

  • Sertac Akyuz
    Sertac Akyuz over 6 years
    Point '3' is an error in documentation. HexToBin accounts for uppercase letters since at least D7 (older version I can check is D3, which doesn't have the function at all).
  • Remy Lebeau
    Remy Lebeau over 6 years
    @SertacAkyuz: you are right, I misread the code when I checked it before posting. FYI, the support of uppercase goes all the way back to at least Delphi 5, if not 4 (but it didn't ignore non-hex characters #58..#64,#91..#96 until Delphi 2009).
  • Remy Lebeau
    Remy Lebeau over 6 years
    @SertacAkyuz: I reported the documentation error: RSP-19625
  • peiman F.
    peiman F. about 5 years
    firemonkey in android dont have PAnsiChar and AnsiString
  • Remy Lebeau
    Remy Lebeau about 5 years
    @peimanF. So just use String and PChar instead. You don't need AnsiString support to use hex strings. Bin2Hex() and Hex2Bin() are overloaded for Unicode strings. Otherwise, install Andreas Hausladen's ByteStrings patch.