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
5989 Views

Similar Articles

[PageSpeed] 17

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

Similar Artilces:

Converting string to Hex
I'm trying to convert a single character, into a Hex value, and compare. Here's my code: dim int as integer int = Integer.Parse(messages.Substring(x, 1), System.Globalization.NumberStyles.HexNumber) If int = &H0 Or int = &HC Then 'Do stuff end if Unfortunately, this causes an error with the character I'm trying to catch. Does anyone have any ideas? int = Integer.Parse(messages.Substring("x", 1), System.Globalization.NumberStyles.HexNumber) View My Blog Download My URL Rewriter and Reverse ProxyOnly $9.95/month, ASP.NET, 2GB & SQL 2005...

String to Linq.Byte and Linq.Byte to String convertion?
 Hi, I have a field in my database as image type. I am using Linq to read/insert records.I am having little hard time inserting text(string) into the image field and then converting back to string.Do you have any suggestions? Thanks in advance,   The below example shows how to convert Linq Binary to byte If Not String.IsNullOrEmpty(HttpContext.Current.Request.QueryString("imageID")) Then Dim imageID = HttpContext.Current.Request.QueryString("imageID") Dim db As New AuctionsDataContext Dim result = From img In db.Image...

How to convert from string to hex number
Hello, I have a requirement for passing a hex value like this to a function. Example code: my $ipmbAddress = getIpmbAddress(...); ## returns 82 getDeviceID($ip, $ipmbAddress); Here in the above code, the getDeviceID() fucntion will accept only 0x82 (hex number). How to convert the number 82 to 0x82 (Note that this should not be as string) I tried the following but not working properly. $ipmbAddress = "0x".$ipmbAddress; $ipmbAddress = hex("$ipmbAddress"); $ipmbAddress = sprintf(0x%0x, $ipmbAddress); getDeviceID($ip, $ipmbAddress); Please help. T...

How to convert MemoryStream string to Hex?
I'm doing some encryption and, normally would return a baes 64 string but it's causing issues with URLs, so we are going to use hex strings instead....but I'm having an issue... Instead of:Return Convert.ToBase64String(ms.GetBuffer(), 0, CInt(ms.Length)) I'd like to return a hex string... I need a pointer here....thanks..... Dim myNumber as integer = 'Get your Number from the buffer Dim HexString as string = myNumber.tostring("X")Bryan SampicaAuction Site StarterKit |My Blog The tough part is the "get your number from the buffer" part.... That's what I don't get/know. I'm trying to...

Converting a Color to a Hex String
How do I convert the value of a Color object to an HTML compatible hexadecimal string ' I have a function called StatusColor that gives back a Color object. When I try this I don't get an HTML hexdecimal color: Dim HtmlColor as String HtmlColor = Hex(SampleRow.StatusColor().ToArgb) Regards, Pat mueslilux:How do I convert the value of a Color object to an HTML compatible hexadecimal string ' I have a function called StatusColor that gives back a Color object. When I try this I don't get an HTML hexdecimal color: Dim HtmlColor as String HtmlColor = Hex(SampleRow.StatusColor()...

Convert binary to hex for query string
I know this may sound strange. But I have run into an issue. The webpage has list box that could have up to 1500 values in it. I am running into roadblocks when a user selects all of the 1500 values to Select from the sql dB. It creates a query string that is to large for the browser. My idea is to run through list box and assembly a string with 0 and 1 then convert the string to binary and then to hex. Passed the hex number to the db and break it down on the other side.   Any thoughts or work around suggestions would be greatly appreciated. thanks,     Why...

convert byte[] to String and string to byte[] note encoding and decoding required....
hi all,        As the subject, can I have solution ofr the same.......  e'i Convert byte[] into string and strting into byte[] , can i have teh sample page.        Thanks & Regards,Murthy.          System.Text.Encoding.Unicode.GetString(ByteArray);//Byte Array to String             System.Text.Encoding.Unicode.GetBytes(String);//String to Byte ArrayWe All move to our original place.Don't forget.Imran Baloch thanks for...

Hex string to ascii string
Does anyone know how to convert a hex string to an ascii string? I don't have a definitive answer for you, but look at the following and see if it helps: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=CuoS%23ssX%2 3GA.289%40forums.powersoft.com&rnum=3&prev=/groups%3Fq%3Dpowerbuilder%2Bconvert% 2Bhex%2Bto%2Bascii%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DCuoS%252 3ssX%2523GA.289%2540forums.powersoft.com%26rnum%3D3 http://www.pfcguide.com/extensions/ext_0005.asp On Tue, 10 Jun 2003 12:37:25 -0400, in powersoft.publi...

Convert Decimal To 2-Bytes Hex Value
Hi all, I want to convert with PB7 a decimal value (ie 1487) to a hex 2-bytes number. Simple question, but does anyone knows how to make it work Thank you in advance Alex /Open Support The easiest way I can think of is to write a small DLL that exports a function like the following: void IntToHex( int input, char * output ) { sprint( output, "%X", input ); } Then, declare a local external function in PB as follows: SUBROUTINE IntToHex( long input, REF string output ) LIBRARY "mysimpledll.dll" Sample PowerScript: String ls_hex l...

Open file in HEX, find and replace HEX, then write back to File in HEX?
I need to create an application similar to a hex editor and can't find how to do this on the internet. I want to be able to open any file (text,doc,mp3 etc) including binary files but I need to display the results in hex. I will search a string (in HEX) and replace it by another string (in HEX). Then write back to file. My program should like this: 1. Open the file in HEX (i think can use TFileStream) and parse to a TMemo or TstringList (or something else could speed up the process) 2. Input a string to FIND (max 12 chars). 3. Input a string to REPLACE (max 12 chars), may be longe...

Converting to Hex
Is there a simple way, like a one liner perhaps, that will give me the octal/hex value of a character? I need to look certain characters up, so I would like to be able to give it something like an 'a' or even "\n" and have it answer 141 or 012 (assuming UNIX). Thanks for your time. James check out sprintf() and ord() quickly for you example: ## hex perl -e 'printf("%x", ord("\n"))' ## oct perl -e 'printf("%o", ord("\n"))' > -----Original Message----- > From: James Edward Gray II [m...

Hex - dec, dec
Hi, is there any powerscript that convert hex - dec? and vice versa? thanks in advance :) James Liew Code for dec to hex (Sorry, don't know the exact algorithm anymore for hex -> dec) LONG ll_number, ll_num, ll_div, ll_dec INTEGER li_rem STRING ls_hex ls_hex = '' ll_dec = 41302 ll_num = ll_dec DO WHILE ll_num > 0 ll_div = ll_num / 16 li_rem = ll_num - (ll_div * 16) ll_num = ll_div CHOOSE CASE li_rem CASE 10 ls_hex = 'A' + ls_hex CASE 11 ls_hex = 'B' + ls_hex CASE 12 ls_hex = 'C' + ls_hex ...

hex to dec and dec to hex
Is there an easy way to go back and forth between decimal and hex in perl. I know of the hex function which solves my hex to dec problem but how about a dec->hex function or module? I need the value which is why printf isn't working for me. Thanks for any help. -Ken ---------------------------------------------------------------------------- -------------------- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. Ken Lehman [mailto:ken_lehman@summithq.com] : : ...

Convert from hex
echo http%3A%2F%2Fwww.merifiles.com%2Fuploads%2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_%28Mama_Told_Me%29%28128Kbps%29.mp3| perl -e ' $_=<>;s/%(..)/\\x$1/xg; print "$_"; ' This outputs http\x3A\x2F\x2Fwww.merifiles.com\x2Fuploads\x2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_\x28Mama_Told_Me\x29\x28128Kbps\x29.mp3 Why does print not print the hex character in question? But, -bash-2.05b$ perl -e ' > print "http\x3A\x2F\x2Fwww.merifiles.com\x2Fuploads\x2Fmf_aR_Ek_Ajnabi_-_01_-_Ek_Ajnabi_\x28Mama_Told_Me\x29\x28128Kbps\x29.mp3" > ' http:/...

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