MathGroup Archive 2009

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

Search the Archive

Re: how to read in a number in hex and convert it to binary

  • To: mathgroup at smc.vnet.net
  • Subject: [mg105220] Re: [mg105176] how to read in a number in hex and convert it to binary
  • From: Carl Woll <carlw at wolfram.com>
  • Date: Wed, 25 Nov 2009 02:29:37 -0500 (EST)
  • References: <200911231153.GAA22070@smc.vnet.net>

Michael O&#39;Hanrahan wrote:
> Hi, newbie question I'm afraid, but I couldn't work out how to do this from the help. I'm using Mathematica 5.2.
>
> I've got a textfile containing about 30 thousand 256-digit numbers in hex. I need to read them in and work on them one by one. Reading them is fine using
>
> Read["hello.txt",Expression]
>
> Then I would like to turn each number into a fixed-length binary number, using something like
>
> IntegerDigits[16^^p, 2, 27]
>
> The problem is, this doesn't work. The argument of ^^ seems to need to be an actual number in hex, e.g. 16^A12B3, rather than a variable.
>
> Given that essentially ^^ is a two-argument function, is there a way to express it as such in Mathematica?
>
> I'm even having trouble getting the input to be accepted as a number at all . Replacing "Expression" (or "String") with "Number" or "Integer" doesn't work.
>
> Will be grateful for any help with this.
> Thanks!
> Michael
>
>   
You could try using FromDigits:

In[73]:= FromDigits["A12B3", 16]

Out[73]= 660147

Then, IntegerIDigits[660147,2,27] will do what you want.

Carl Woll
Wolfram Research


  • Prev by Date: Re: how to find a function
  • Next by Date: Re: Bug in Plot3D?
  • Previous by thread: Re: how to read in a number in hex and convert it to
  • Next by thread: Re: how to read in a number in hex and convert it to binary