Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 13 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 stringToHex function is converting a hexadecimal to decimal. The string argument passed contains the hex digits. Therefore, the name of the function should be changed to something like hexStringToDecimal. But this is already implemented with FromDigits[hexString, 16] as halirutan said. – Athanassios Jun 20 '15 at 0:22

Your Answer


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.