I know that to change a single number from hexadecimal representation to decimal, you can use: base^^digits.

However, if you have a list of numbers, Mathematica seems to treat the list name as the number, rather than as an item in a list.

For example:

16^^56 = 86


u={56, 57, 58};
16^^u[[1]] = error

With the error message: General::digit: "Digit at position 1 in u is too large to be used in base 16."

So it's trying to literally convert u[[1]] to hexadecimal!

I've tried using ToExpression[u[[1]]], but that didn't help. How do I convert a list of numbers from hexadecimal to decimal? Thanks!

  • 2
    $\begingroup$ Probably a bad idea to hex a decimal anyway. $\endgroup$ – Daniel Lichtblau Mar 31 '15 at 1:05
  • $\begingroup$ @Daniel Lichtblau that joke is way past its prime. $\endgroup$ – PlaysDice Mar 31 '15 at 2:30

The ^^ syntax is only used for inputting literals. You want to use FromDigits, e.g.

FromDigits[#, 16] & /@ {"56", "57", "58"}

Note that the input numbers must be strings. 56 is a decimal number only. In order to input numbers with higher or lower bases you must use a string (i.e. "56").

  • 1
    $\begingroup$ To automate this little more, may be FromDigits[#, 16] & /@ (ToString[#] & /@ u) $\endgroup$ – Nasser Mar 31 '15 at 0:51

Using the new Interpreter in ver10+

listhex = {"56", "7f", "ff"};

(* Out {86, 127, 255} *)

and this can be coupled with ToString a la Nasser

The Interpreter function seems to have the undocumented(?) ability to act directly on "Ox.." format e.g.

fromHexString[value_] := Interpreter["HexInteger"][value];
fromHexString @ {"0x4f", "f", "0x80", "0x83", "83", "f2", "c6", "d2", "60", "16"}

(*Out {79, 15, 128, 131, 131, 242, 198, 210, 96, 22} *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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