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

*To*: mathgroup at smc.vnet.net*Subject*: [mg105211] Re: [mg105176] how to read in a number in hex and convert it to*From*: DrMajorBob <btreat1 at austin.rr.com>*Date*: Tue, 24 Nov 2009 05:50:36 -0500 (EST)*References*: <200911231153.GAA22070@smc.vnet.net>*Reply-to*: drmajorbob at yahoo.com

You can't fit 256 hex digits into 27 binary digits, but other than that... Here's an example. First I'll create a much smaller "hello.txt" file... 30 numbers, each with exactly 5 hex digits: str = OpenWrite["hello.txt"]; Scan[WriteString[str, # <> "\n"] &, IntegerString[RandomInteger[{16^4, 16^5 - 1}, 30], 16]] Close[str]; Next, I'll read in the data as a list of strings: str = OpenRead["hello.txt"]; strings = ReadList[str, String]; Close[str]; strings {"21938", "7dc12", "beaa2", "b96c6", "d3975", "6eb8f", "71ab8", \ "dadea", "2f74a", "db7dd", "40d67", "88802", "21f47", "8afdb", \ "41202", "21a69", "f1db6", "b2032", "c2cea", "dfd08", "5d977", \ "3a45c", "98845", "4c00c", "defb3", "c9889", "dc69a", "b0dbb", \ "7d803", "c9c3a"} These will all fit into 20 binary bits, but I'll pad them to 21 just for fun. (binaries = IntegerDigits[FromDigits[#, 16], 2, 21] & /@ strings) // Column {0,0,0,1,0,0,0,0,1,1,0,0,1,0,0,1,1,1,0,0,0} {0,0,1,1,1,1,1,0,1,1,1,0,0,0,0,0,1,0,0,1,0} {0,1,0,1,1,1,1,1,0,1,0,1,0,1,0,1,0,0,0,1,0} {0,1,0,1,1,1,0,0,1,0,1,1,0,1,1,0,0,0,1,1,0} {0,1,1,0,1,0,0,1,1,1,0,0,1,0,1,1,1,0,1,0,1} {0,0,1,1,0,1,1,1,0,1,0,1,1,1,0,0,0,1,1,1,1} {0,0,1,1,1,0,0,0,1,1,0,1,0,1,0,1,1,1,0,0,0} {0,1,1,0,1,1,0,1,0,1,1,0,1,1,1,1,0,1,0,1,0} {0,0,0,1,0,1,1,1,1,0,1,1,1,0,1,0,0,1,0,1,0} {0,1,1,0,1,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,1} {0,0,1,0,0,0,0,0,0,1,1,0,1,0,1,1,0,0,1,1,1} {0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0} {0,0,0,1,0,0,0,0,1,1,1,1,1,0,1,0,0,0,1,1,1} {0,1,0,0,0,1,0,1,0,1,1,1,1,1,1,0,1,1,0,1,1} {0,0,1,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,0} {0,0,0,1,0,0,0,0,1,1,0,1,0,0,1,1,0,1,0,0,1} {0,1,1,1,1,0,0,0,1,1,1,0,1,1,0,1,1,0,1,1,0} {0,1,0,1,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,1,0} {0,1,1,0,0,0,0,1,0,1,1,0,0,1,1,1,0,1,0,1,0} {0,1,1,0,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0} {0,0,1,0,1,1,1,0,1,1,0,0,1,0,1,1,1,0,1,1,1} {0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,0,1,1,1,0,0} {0,1,0,0,1,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,1} {0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0} {0,1,1,0,1,1,1,1,0,1,1,1,1,1,0,1,1,0,0,1,1} {0,1,1,0,0,1,0,0,1,1,0,0,0,1,0,0,0,1,0,0,1} {0,1,1,0,1,1,1,0,0,0,1,1,0,1,0,0,1,1,0,1,0} {0,1,0,1,1,0,0,0,0,1,1,0,1,1,0,1,1,1,0,1,1} {0,0,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,1,1} {0,1,1,0,0,1,0,0,1,1,1,0,0,0,0,1,1,1,0,1,0} I hope that helps. Bobby On Mon, 23 Nov 2009 05:53:11 -0600, Michael O'Hanrahan <hanrahan398 at yahoo.co.uk> 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 > -- DrMajorBob at yahoo.com

**References**:**how to read in a number in hex and convert it to binary***From:*"Michael O'Hanrahan" <hanrahan398@yahoo.co.uk>

**Re: A little C program calling MathLink**

**Re: Kernel crashes in ReplaceAll - with or without Maximize failure**

**how to read in a number in hex and convert it to binary**

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