14
$\begingroup$

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

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

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.

Initial answer

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:

enter image description here

$\endgroup$
  • $\begingroup$ Learned a lot from this. Thanks! (+1) $\endgroup$ – Edmund Dec 9 '17 at 17:22
  • $\begingroup$ 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. $\endgroup$ – Edmund Dec 9 '17 at 17:32
  • $\begingroup$ @Edmund See update. $\endgroup$ – Carl Woll Dec 9 '17 at 17:34
  • 1
    $\begingroup$ 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? $\endgroup$ – Edmund Dec 9 '17 at 17:36
  • 1
    $\begingroup$ @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. $\endgroup$ – Carl Woll Dec 9 '17 at 18:14
11
$\begingroup$

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

Mathematica graphics

In a new cell type Esc addia Esc

This converts to the add input alias template.

Mathematica graphics

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

Mathematica graphics

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.

enter image description here

Hope this helps.

$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Mark Dec 9 '17 at 13:46
  • 3
    $\begingroup$ @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. $\endgroup$ – Edmund Dec 9 '17 at 13:54
  • $\begingroup$ 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 ! $\endgroup$ – Mark Dec 9 '17 at 14:00
3
$\begingroup$

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}$

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

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.