# Convert a list of hexadecimal numbers to decimal

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


But:

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!

• Probably a bad idea to hex a decimal anyway. Mar 31, 2015 at 1:05
• @Daniel Lichtblau that joke is way past its prime. Mar 31, 2015 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").

• To automate this little more, may be FromDigits[#, 16] & /@ (ToString[#] & /@ u) Mar 31, 2015 at 0:51

Using the new Interpreter in ver10+

listhex = {"56", "7f", "ff"};
Interpreter["HexInteger"]@listhex

(* 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} *)