MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Re: converting hex strings

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27479] RE: [mg27456] Re: converting hex strings
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
  • Date: Tue, 27 Feb 2001 00:37:32 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

for my comment see below

-----Original Message-----
From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
Subject: [mg27479] [mg27456] Re: converting hex strings


Maarten,
ToExpression["16^^"<>#]&/@{"01a0","ffe","0080"}

        {416,4094,128}

You have one f too many.
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

<maarten.vanderburgt at icos.be> wrote in message
news:97a7b2$aks at smc.vnet.net...
> 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
> -------------------------------------------
>
>
>
--------------------
[Hartmut Wolf]

Maarten,

as you see from

In[17]:= 16^^a1b0 // BoxForm
Out[17]= "41392"

(the output is a string!) ^^ is a evaluated when parsing the textual 
input, it is not a Mathematica function (hasn't even got a name).
Conversion from text may be called by ToExpression, so Allan's form follows.

As for the IEEE floating point standard I recommend to visit
http://www.psc.edu/general/software/packages/ieee/ieee.html


For that we first need a binary representation

In[36]:=
HexToBinDigits = (IntegerDigits[#, 2, 32] &) /@ (ToExpression["16^^" <> #]
&)

In[37]:=
(exa = HexToBinDigits /@ {"42c80000", "3dfcd35b", "bf800000"}) // 
  TableForm[#, TableSpacing -> {1, 0}] &

compare that with

In[38]:=
BaseForm[#, 2] & /@ (ToExpression["16^^" <> #] &) /@ {"42c80000",
"3dfcd35b", 
        "bf800000"} // TableForm[#, TableAlignments -> Right] &

We now may implement what we have learnt from psc.edu:

In[39]:=
FromIEEE32Digits[nn_] := 
  With[{s = First[nn], 
    e = FromDigits[Take[nn, {2, 9}], 2], 
    f = FromDigits[Drop[nn, 9], 2]}, 
   Which[e == 255 && f != 0, NaN, 
    e == 255 && f == 0 && s == 1, -Infinity, 
    e == 255 && f == 0 && s == 0, Infinity, 
    0 < e < 255, (-1)^s*2^(e - 127)*
     (1.`7.22472 + N[f/2^23, 7.22472]), 
    e == 0 && f != 0, 
    ((-1)^s*(0 + N[f/2^23, 7.22472]))/2^126, 
    e == 0 && f == 0 && s == 1, -zero, 
    e == 0 && f == 0 && s == 0, 0]]

As you see it is not quite possible to truely implement the 
IEEE 32 bit Standard with Mathematica.

You now may try the examples from the web page cited,
and also your's:

In[56]:=
FromIEEE32Digits /@ exa
Out[56]=
{100., 0.12345, -1.}

-- Hartmut



  • Prev by Date: out of memory, again.
  • Next by Date: Re: What is the difference between z[x_]:= D[y[x],x] and z[x_]:=y'[x]?
  • Previous by thread: Re: Re: converting hex strings
  • Next by thread: Boundary value problem with NDSolve