Re: converting hex strings
- To: mathgroup at smc.vnet.net
- Subject: [mg27502] Re: converting hex strings
- From: Roland Franzius <Roland.Franzius at uos.de>
- Date: Thu, 1 Mar 2001 03:53:28 -0500 (EST)
- Organization: Universitaet Osnabrueck
- References: <97a7b2$aks@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
as Paul and Allen pointed out, mapping the string function
ToExpression["16^^"<>#]&
onto the elements of the list gives their hex equivalent numbers.
Another way, convert the string into a list of hex digits
ToCharacterCode[#]-48&
and Apply a
Fold[ Plus[#1*16, #2], 0, list ]&
to the part of the list representing mantissa and exponent.
There are three IEEE formats.
Single (32bit)(largely obsolet with fpu):
bit 0-22: 23bit left justified mantissa, 1.x in the range 1.0-2.0, the
1.bit is discarded,
bit 22-30: 8 bit 2-exponent + 127 = 2^7-1 ,
bit 31: sign
double(64bit)
bit 0-51: 52bit left justified mantissa 1.xin the range 1.0-2.0, the
1.bit is discarded,
bit 52-62: 11 bit 2-exponent + 1023 = 2^10-1
bit 63: sign
extended precision (80bit)
bit 0-63: left justified mantissa 1.x with explicit 1. bit at position
63
bit 64-79: zero
bit 80-94: 15bit 2-exponent + 16383 = 2^14-1
bit 95: sign
regards
maarten.vanderburgt at icos.be wrote:
>
> Hallo,
>
> I have a list of hexadecimal strings (read from a hex string text file):
> e.g. {"01a0", "fffe", "0080"} representing integers.
> How can I convert such a list to integers so that I get {416, 4094, 128}?
> I cannot make a function using something like
>
> hextoint = 16^^#&
>
> or
>
> hextoin = 16^^ToExpression[#]&
>
> I get errors like below:
>
> In[44]:= hexstr = "a1b0"
> t = ToExpression[hexstr];
> 16^^t
> General::"digit": "Digit at position \!\(1\) in \!\(\"t\"\) is too large to
> \
> be used in base \!\(16\)."
>
> Where does this error come from?
>
> While this works ok:
> In[43]:= 16^^a1b0
> Out[43]= 41392
>
> The ToExpression[ ] also would give problems: ToExpression["01a0"] returns
> 0.
>
> Has anyone done anything along these lines?
>
> How would I have to convert a hex list representing floats (IEEE floating
> point standard)?, e.g. {"42c8000", "3dfcd35b", "bf80000"} which should
> convert to {100.0, 0.12345, -1.0} in a decimal notation.
>
> Thanks for your help
>
> Maarten
>
> -------------------------------------------
> Maarten van der Burgt
>
> Icos Vision Systems
> Esperantolaan 9
> B-3001 Leuven
> Belgium
> -------------------------------------------
--
Dr. Roland Franzius
Theor. Physik FB Physik, Univ. Osnabrueck
tel ++49 +541 969 2683 fax ++49 +451 969 2670
+++ exactly <<n>> lines of this message have value <<FALSE>> +++