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. Follow

2 Replies
10036 Views

Similar Articles

[PageSpeed] 4
Get it on Google Play
Get it on Apple App Store

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)
1
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
Reply:

Web resources about - how to convert hex string to hex bytes? - embarcadero.delphi.firemonkey

List of converts to Buddhism - Wikipedia, the free encyclopedia
Ajahn Brahm (born 1951), Religious teacher and student of the late Thai monk Ajahn Chah . Currently he is the abbot of the Bodhinyana Monastery, ...

Facebook Timeline For Pages: 8 Million Converts, 29 Million To Go
... reach to 98 percent of users who liked its page, as well as to double its engagement rate, during a 28-day period. Dr. Pepper hasn’t yet converted ...

Facebook stock roundup: Execs offload more stock, directors convert shares
... Bowles, the former co-chair of the now-defunct National Commission on Fiscal Responsibility and Reform, and Reed Hastings, Netflix CEO, converted ...

How to Convert Leads Into Buying Customers - The 9 Step Sales Process EVERYONE Should Use
... School of Business in Toronto on the topics of Sales and Entrepreneurship. I shared with them the sales process that I find How to Convert Leads ...

Bitcoin : Should I convert my savings to Bitcoin?
Answer (1 of 7): You should think of this question as "Should I invest all my savings in Bitcoin?", and not "Should I have my savings denominated ...

Coordinates - Calculate and Convert a Position on the App Store on iTunes
Get Coordinates - Calculate and Convert a Position on the App Store. See screenshots and ratings, and read customer reviews.

Catholics should not convert Jews, says Vatican, in push against anti-Semitism
... 50 years since the Vatican's repudiation of the concept of Jewish guilt for Jesus' death.  Vatican City: Catholics should not try to convert ...

Today’s apps gone free: BlockBear, Stock Grader, PDF Smart Convert and more
Block ads and other annoying content, take the emotion out of investing, and convert almost anything into a PDF with todays collection of apps ...

Ford patents rear wheel that converts into a unicycle
Filed under: Government/Legal , Green , Ford , Green Driving , Motorcycle , Electric Ford is thinking about allowing drivers to remove a rear ...

How to convert your old VHS tapes to DVD with this software
Remember those old VHS tapes you have sitting in the corner? Convert them into DVDs with this affordable software so you can easily watch them ...

Resources last updated: 1/19/2016 7:53:29 AM