Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

68 views
Skip to first unread message

DrMajorBob

unread,
Nov 24, 2009, 5:50:55 AM11/24/09
to
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&#39;Hanrahan
<hanra...@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
>


--
DrMaj...@yahoo.com

Carl Woll

unread,
Nov 25, 2009, 2:39:23 AM11/25/09
to
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

0 new messages