Re: convert hex string to ascii [message #53366 is a reply to message #53362] |
Sat, 07 April 2007 11:41  |
Craig Markwardt
Messages: 1869 Registered: November 1996
|
Senior Member |
|
|
"metachronist" <rkombiyil@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: craigmnet@REMOVEcow.physics.wisc.edu
Astrophysics, IDL, Finance, Derivatives | Remove "net" for better response
------------------------------------------------------------ --------------
|
|
|