4

I want to convert a hexadecimal string to a decimal (base 10) number:

16^^ff
16^^ToExpression["ff"]

The 1st line returns the correct answer of 255, while the 2nd line gives an error. Why doesn't it work?

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
user8454
  • 353
  • 2
  • 9
  • 6
    It is not correct to say (or, rather, imply) that the sigil ^^ corresponds to BaseForm, although I grant you that this is referenced in the BaseForm documentation. It is just another notation for entering numbers, like 1*^10. These particular notations are processed by the parser, are not interpreted as functions, and require that numeric literals appear both on the left and the right. It doesn't work since ToExpression["ff"] is not a numeric literal. You may use e.g. ToExpression@ToString@StringForm["16^^``", "ff"] instead. – Oleksandr R. Feb 02 '14 at 18:09
  • Oleksandr, What do the two grave accents mean in this case? I've seen them before to indicate accuracy, as in Accuracy[1.2``20] – DavidC Feb 02 '14 at 18:21
  • @DavidCarraher It is just StringForm's syntax... The second argument to StringForm gets inserted where ever there's a double backtick. If you have more than one argument to insert, you can indicate the order with a number, like `1`. – rm -rf Feb 02 '14 at 18:28
  • See here: http://mathematica.stackexchange.com/a/39753/4346 – Greg Hurst Feb 02 '14 at 19:42
  • 6
    Perhaps you have good reasons to use ToExpression but FYI the usual method to interpret a hexadecimal string is with FromDigits, e.g. FromDigits["FF", 16] gives 255 – Simon Woods Feb 02 '14 at 20:56
  • It doesn't work because 16^^ff is interpreted as parse time. It doesn't go like input -> compound expression -> evaluation. It is directly parsed into an atomic integer. 16^ff is just a different way to write the atomic 255, there's no difference between the two inputs from Mathematica's point of view. You might have been under the impression that ^^ is an operator that might correspond to some head, so you can join two arbitrary expressions using this operator. This is not the case. ^^ is not an operator in Mathematica, it's just part of the syntax for writing numbers ... – Szabolcs Feb 03 '14 at 01:41
  • ... the same way as the decimal point is not an operator, just a part of writing numbers. The same goes for the backtick, double backtick, and *^, which can also be used in inputting numbers. – Szabolcs Feb 03 '14 at 01:44
  • Seems to me that comments should be converted to answers and rep awarded for this discussion. – bobthechemist Feb 03 '14 at 03:39

1 Answers1

2

As already stated in the comments the ^^ notation is handled in parsing; observe:

HoldComplete[16^^ff] // FullForm
HoldComplete[255]

(I intend this to illustrate that this notation is "evaluated" before the main evaluator ever sees it.)

This parsing is really no different from other numerical notation in Mathematica, for example 12.345 is directly parsed as a Real number, not an expression involving Dot. Likewise 1*^6 is parsed as the Integer one million, with no relation to Times or Power.

As Simon Woods recommended, for programmatic input of hexadecimals use FromDigits:

FromDigits["ff", 16]
255
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371