Results 1 to 4 of 4

Math Help - Mathematica - how to read in a number in hex and convert it to binary?

  1. #1
    Newbie
    Joined
    Aug 2008
    Posts
    3

    Mathematica - how to read in a number in hex and convert it to binary?

    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
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Super Member
    Joined
    Aug 2008
    Posts
    903
    Here's one idea: I save three hex numbers into a file as strings, then read them back as strings, then do the binary conversion:

    Export["myHexFile.txt", {"f", "abc03b", "100fab"}]
    myHexList = ReadList["myHexFile.txt", String]
    IntegerDigits[FromDigits[#], 2, 27] & /@ myHexList

    You may need to work on this a bit to get it to work for your application depending how you have the numbers formatted in the file. I'm not sure about the 16^^num format. Also, the last command is a "mapping" which takes each element in MyHexList, and passes it to the IntegerDigit routine at the placeholder #. See "Map" command.
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Aug 2008
    Posts
    3
    Quote Originally Posted by shawsend View Post
    Here's one idea: I save three hex numbers into a file as strings, then read them back as strings, then do the binary conversion:

    Export["myHexFile.txt", {"f", "abc03b", "100fab"}]
    myHexList = ReadList["myHexFile.txt", String]
    IntegerDigits[FromDigits[#], 2, 27] & /@ myHexList

    You may need to work on this a bit to get it to work for your application depending how you have the numbers formatted in the file. I'm not sure about the 16^^num format. Also, the last command is a "mapping" which takes each element in MyHexList, and passes it to the IntegerDigit routine at the placeholder #. See "Map" command.
    Hi, thanks for this. Still doesn't work though. My data is a list of about 30 thousand 256-character hex numbers, separated by newlines, which I need to read in one by one. I can do this using

    Read["hello.txt",Expression]

    or

    Read["hello.txt",String]

    I seem to be getting into a complete mess. First, 16^^ doesn't seem to be able to take a variable as its argument. (Is there a way to use standard Mathematics function notation for ^^, using square brackets? It's surely a function in all but name, e.g.16^^A = 10 essentially means function(16, "A")= the decimal number 10, unless I'm wrong).

    Second, how do I get the number I've read in to be accepted as an integer? I've tried replacing "Expression" with "Number" and "Integer", but that doesn't work. I can assign the input to a variable, e.g.

    Read["hello.txt",String]
    p=%

    although then if I do something like

    p+1

    I just get

    1 + A03B2174381BAC01025.......



    Thanks again.

    Michael
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Super Member
    Joined
    Aug 2008
    Posts
    903
    Read returns a string with "string" specified so 1+mynum is 1 plus a string. Use FromDigits to convert it to a number:

    So I put three hex numbers separated by new lines into a text file "secondtextfile.txt" using just Notepad this time:

    ffaa10345
    aa1234b
    cc10ff

    Then used the following code to read the first one in:

    Code:
    mynum = Read["secondtextfile.txt", String]
    1 + mynum
    myrealnum = FromDigits[mynum, 16]
    1 + myrealnum
    Which gave me:

    "ffaa10345"

    1 + "ffaa10345"

    68629365573

    68629365574
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 2
    Last Post: May 3rd 2011, 04:22 PM
  2. [SOLVED] How do you convert from cos(pi/number)
    Posted in the Trigonometry Forum
    Replies: 6
    Last Post: September 15th 2010, 05:19 PM
  3. convert single numbers to 5 bit binary
    Posted in the Number Theory Forum
    Replies: 4
    Last Post: June 28th 2010, 06:18 PM
  4. Replies: 5
    Last Post: June 3rd 2010, 04:32 AM
  5. Convert the decimal number
    Posted in the Algebra Forum
    Replies: 5
    Last Post: December 17th 2009, 05:20 AM

Search Tags


/mathhelpforum @mathhelpforum