# How can I tell Mathematica to interpret 0xffff as a hexadecimal number?

I am stuck. I would like Mathematica simply accept 0x as a prefix Operator for Hexadecimal Numbers. I know that 0x is interpreted as 0*x, so maybe this is just not possible. I am a programmer and am so used to type something like 0xffff instead of 16^^ffff ...

• There is a topic about conversion from string if that is fine with you. – Kuba Dec 9 '17 at 12:56
• Closely related: How to convert a hex color string to RGBColor? – Kuba Dec 9 '17 at 14:56
• @Alexey: Thanks for the edit ! – Mark Dec 10 '17 at 14:53

Update

As pointed out by @Edmund, my initial answer didn't work with hex numbers starting with an integer. To fix that, I included an initial \[DiscretionaryHyphen] character, and then I drop that character when converting to a number using FromDigits (my first update used x, but I like this new approach better):

CurrentValue[EvaluationNotebook[], InputAliases] = {
"0x" -> RowBox[{
InterpretationBox[
StyleBox["\"0x\"", "Inactive", ShowStringCharacters->False],
Function[Null, FromDigits[StringDrop[ToString@Unevaluated@#, 1], 16], HoldAll]
],
"\[InvisibleApplication]",
"\[InvisibleSpace]",
StyleBox["\[DiscretionaryHyphen]", ShowAutoStyles->False]
}],
ParentList
};


Edmund also points out that you can use my original approach, you just need to include quotes if the hex number starts with an integer.

If you are satisfied with an InputAliases approach, you could try:

CurrentValue[EvaluationNotebook[], InputAliases] = {
"0x" -> RowBox[{
InterpretationBox[
StyleBox["\"0x\"", "Inactive", ShowStringCharacters -> False],
Function[Null, FromDigits[ToString@Unevaluated@#, 16], HoldAll]
],
"\[InvisibleApplication]",
"\[InvisibleSpace]"
}],
ParentList
};


Here is an animation showing the alias in action:

• Learned a lot from this. Thanks! (+1) – Edmund Dec 9 '17 at 17:22
• Your usage wipes all the existing input aliases out of the InputAliases option. Perhaps you could update to append this the existing list of input aliases. – Edmund Dec 9 '17 at 17:32
• @Edmund See update. – Carl Woll Dec 9 '17 at 17:34
• Humm, there seems to be an issue with this solution when the hex numbers do not begin with a character. It works for f6 but not 6f. Is there a way to fix this? Well, you can just do "6f" in these cases. Perhaps best to always use the quotes? – Edmund Dec 9 '17 at 17:36
• @Edmund I added a version that starts the hex number with x so that mixtures of integers and letters can be added on. Then the interpretation function drops the x and converts to an integer. – Carl Woll Dec 9 '17 at 18:14

One approach is to the use that Notation Package's AddInputAlias function to setup an alias that will convert Esc 0x Esc to 16^^ when you type it.

First load the notation package with

Needs["Notation"]


You can then view all the active notation aliases with

ActiveInputAliases[]


One of these in the list is an input alias to add input alias (addia).

In a new cell type Esc addia Esc

This converts to the add input alias template.

Enter 0x in the quoted box and 16^^ in the template box.

Then evaluate the cell.

Now evaluating ActiveInputAliases[] again will show the new 0x alias in the list.

To use simply type Esc 0x Esc in any expression and it will be converted to 16^^. Then just type your hexadecimal number.

Hope this helps.

• Thank you very much Edmund, this works, but then I have to type 'Esc' 0x 'Esc' and this then converts automatically to 16^^. I want the Expression to stay in the form 0xabcd and evaluated like usual, e.g. as a hexadecimal number. – Mark Dec 9 '17 at 13:46
• @Mark I think you will be hard pressed to have that result because the operator you want to use 0x starts with a digit and the WolframLanguage does not like that. Perhaps one of the gurus around here can do something fancy with boxes and Formal. This will most likely add extra steps for others to work with your code. In the end you many have to simply accept the syntax of the Wolfram Language. – Edmund Dec 9 '17 at 13:54
• I think you are right. I just was hoping to find some simple solution, but I guess I will just have to use 16^^. Thank you very much ! – Mark Dec 9 '17 at 14:00

In the meantime, I propose to work with strings:

Hex[x_?StringQ] := "16^^" <> StringJoin@Take[Characters@x, {3, Length[Characters@x]}]


So:

Hex@"0xff065"   (* 16^^ff065 *)
ToExpression@Hex["0xff065"]   (* 1044581 *)


and

BaseForm[1044581, 16]
`

$\text{ff065}_{16}$

• Thank you José, not yet what I want but another way to think about it ! – Mark Dec 9 '17 at 13:09