Re: convert hex string to ascii [message #53362] |
Sat, 07 April 2007 17:44  |
rkombiyil
Messages: 59 Registered: March 2006
|
Member |
|
|
On Apr 8, 3:41 am, Craig Markwardt
<craigm...@REMOVEcow.physics.wisc.edu> wrote:
> "metachronist" <rkombi...@gmail.com> writes:
>> Dear list members,
>
>> Sorry to bother with a trivial (?) question.. I was wondering if there
>> is a way to convert a hex byte string to corresponding ascii string?
>> i.e., 'ABC ' would be coded as:41424320 (long int), I need to convert
>> this back as ascii..
>> What I did was to read in the corresponding long integer
>> representation and then use the "Z" format specifier to get the hex
>> representation and look up the "ASCII" table for the characters. But
>> in this case, the order in which these (ascii characters comprising
>> the string) appear reversed (?!, i.e., 'ABC ' as ' CBA'), which I
>> suspect is due to the way hex is computed from its corresponding
>> binary representation?
>
>> I am confused. Any suggestions / where can I find some examples to
>> clear up this hex muddle in my head? :-(
>
> I would have decoded your actual data to a string with,
> data = '41424320'xl
> str = string(byte(data,0,4))
>
> However, your real problem is an "endian" (byte-order) problem. For
> example, the integer '41424320'xl can be stored in two different byte
> strings, depending on the CPU architecture. You need to find out what
> endianness your data was encoded with, and what endianness your
> computer is. In all likelihood you can use BYTEORDER to fix the
> problem. A more brutish solution could be achieved with REVERSE() in
> the expression above.
>
> Good luck,
> Craig
>
> P.S. On my Mac, STR = 'ABC '. On my PC, STR = ' CBA'
>
> --
> ------------------------------------------------------------ --------------
> Craig B. Markwardt, Ph.D. EMAIL: craigm...@REMOVEcow.physics.wisc.edu
> Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
> ------------------------------------------------------------ --------------
Hi Craig,
Thanks for the help! In fact, I have checked the endian-ness before I
posted with /swap_if_little_endian. On my intel machine, I got garbage
for known values like year. I was confused because only the strings
seem to be reversed. The other values (like year,day,or the plots of
data) looked right.. So I was wondering if there is any mismatch
between encoding and endian-ness. I reset all sessions and tried again
- now the string looks alright and no reversing?!! WEIRD! Looks like
there is a lateral swapping going on within my brain :P
Thanks for taking time off to reply. I appreciate it.
~rk
|
|
|