how to convert hex string to hex bytes?

Hi I've been trying and haven't found a solution for this yet and hopefully some of you can give me a direction to solve this problem.

I have a string in hex representation i.e, 64 chars  'AB23FFFE.....'
How can I convert it to 32 chars String where each byte is $AB $23 $FF $FE .... ?
Even if I do it manually, can I store the hex(non ASCII) to String?


Thanks for looking!!
-1
edwin
1/10/2013 6:35:59 PM
📁 embarcadero.delphi.firemonkey
📃 4901 articles.
⭐ 4 followers.

💬 2 Replies
👁️‍🗨️ 6936 Views

edwin wrote:

> I have a string in hex representation i.e, 64 chars  'AB23FFFE.....'
> How can I convert it to 32 chars String where each byte is $AB $23 $FF
> $FE .... ?

The RTL has several overloaded HexToBin() functions available in the System.Classes 
unit.

> Even if I do it manually, can I store the hex(non ASCII) to String?

Why do you need them in a String?  String uses Unicode characters, but what 
you are asking for is not Unicode data.  You should use TBytes (or other 
suitable binary container) instead, eg:

{code}
var
  Hex: String;
  Bytes: TBytes;
begin
  Hex := 'AB23FFFE.....';
  SetLength(Bytes, Length(Hex) div 2);
  HexToBin(PChar(Hex), Bytes[0], Length(Bytes));
  ...
end;
{code}

If you absolutely need a String containing characte representations of the 
raw bytes, you can do this:

{code}
var
  Hex: String;
  Bytes: TBytes;
  I: Ineger;
  S: String;
begin
  Hex := 'AB23FFFE.....';
  SetLength(Bytes, Length(Hex) div 2);
  HexToBin(PChar(Hex), Bytes[0], Length(Bytes));
  SetLength(S, Length(Bytes));
  for I := 0 to Length(Bytes)-1 do
    S[I+1] := Char(Bytes[I]);
  ...
end;
{code}

--
Remy Lebeau (TeamB)
0
Remy
1/10/2013 7:21:16 PM
Thank you so much Remy.  I'm going to give it a try.
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> edwin wrote:
> 
> > I have a string in hex representation i.e, 64 chars  'AB23FFFE.....'
> > How can I convert it to 32 chars String where each byte is $AB $23 $FF
> > $FE .... ?
> 
> The RTL has several overloaded HexToBin() functions available in the System.Classes 
> unit.
> 
> > Even if I do it manually, can I store the hex(non ASCII) to String?
> 
> Why do you need them in a String?  String uses Unicode characters, but what 
> you are asking for is not Unicode data.  You should use TBytes (or other 
> suitable binary container) instead, eg:
> 
> {code}
> var
>   Hex: String;
>   Bytes: TBytes;
> begin
>   Hex := 'AB23FFFE.....';
>   SetLength(Bytes, Length(Hex) div 2);
>   HexToBin(PChar(Hex), Bytes[0], Length(Bytes));
>   ...
> end;
> {code}
> 
> If you absolutely need a String containing characte representations of the 
> raw bytes, you can do this:
> 
> {code}
> var
>   Hex: String;
>   Bytes: TBytes;
>   I: Ineger;
>   S: String;
> begin
>   Hex := 'AB23FFFE.....';
>   SetLength(Bytes, Length(Hex) div 2);
>   HexToBin(PChar(Hex), Bytes[0], Length(Bytes));
>   SetLength(S, Length(Bytes));
>   for I := 0 to Length(Bytes)-1 do
>     S[I+1] := Char(Bytes[I]);
>   ...
> end;
> {code}
> 
> --
> Remy Lebeau (TeamB)
0
edwin
1/10/2013 7:25:53 PM