Re: how to read in a number in hex and then get it in binary?
- To: mathgroup at smc.vnet.net
- Subject: [mg105219] Re: how to read in a number in hex and then get it in binary?
- From: dh <dh at metrohm.com>
- Date: Wed, 25 Nov 2009 02:29:26 -0500 (EST)
- References: <hedspu$l3d$1@smc.vnet.net>
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 a long list of numbers in hex. I can > read them in one by one, using e.g. > > p=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. > > Will be grateful for any help with this. > Thanks! > Michael > Hi Michael, do you really have a file in HEX, not a binary file? Assuming your file is in hex, you could import it as String. Then you need to split it into single hex numbers with the correct length. Having the hex numbers separated you would convert them into decimal Integers using FromDigits. A binary file could be imported directly using e.g. Import Daniel