Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can I convert a string containing a hex number such as "6b" to a hex digit that mathematica can use to do math with?

I have tried using ToExpression["6b"] but that just give me another string.

Note: Java's BigIntegerclass can construct hex integers from strings. Is there something like this in mathematica?

share|improve this question
    
By the way, the returned value is not another string. It's the expression 6 b. If you do FullForm[ToExpression["6b"]] you will see its true form. One thing that makes Mathematica unusual as far as programming languages go is that unassigned symbols (like b in this case) have their own values. They aren't strings and they aren't "variables". They're symbols and they can be passed around like any other value. In Java-speak you can think of them as sort of like enums. –  amr Sep 25 '12 at 17:28

2 Answers 2

up vote 10 down vote accepted

The function converting strings to integer is FromDigits. It is the counterpart of IntegerString and both functions can be used with whatever basis you like. Therefore, if you want to convert from base 16 you do

FromDigits["6b", 16]
share|improve this answer
stringToHex[str_] := ToExpression["16^^" <> str];

This is just a way of automating the normal notation you would use, which is 16^^6b (check here for the documentation).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.