Skip to main content

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!



Answer



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").


Comments