0

We can insert a number in a basis different than 10 using the basis^^number expression. However, strangely enough, there does not seem to be a corresponding functional form to do this. Moreover, to convert a number of inputs to decimal basis, the common syntax

2^^ # & /@ {0, 11, 10, 0} 

does not work, and I have to use instead an expression of the form

ToExpression["2^^" <> ToString@#] & /@ {0, 11, 10, 0}

This, however, seems more like a workaround than a solution.

Why does the former syntax not work? And why is there no functional form of the basis^^number short syntax?

glS
  • 7,623
  • 1
  • 21
  • 61
  • BaseForm is the functional form and appears to work with Map in the way you've described above. – IPoiler Jan 12 '16 at 19:21
  • Are you aware of FromDigits[#,2]& : FromDigits[#, 2] & /@ {{0, 0}, {1, 1}, {1, 0}, {0, 0}}gives : {0, 3, 2, 0} ? – andre314 Jan 12 '16 at 19:22
  • @IPoiler BaseForm, to my knowledge, only converts from decimal to another basis. How do you make it work the other way around, without using the base^^number syntax? – glS Jan 12 '16 at 19:23
  • @glS Isn't that what he's trying to do? If you have a number that's already in another base that you want converted to decimal then you just BaseForm[#,10]. For example, BaseForm[2^^10,10] gives 2. – IPoiler Jan 12 '16 at 19:27
  • @andre I was aware of FromDigits, but I didn't notice before the possibility of using as in FromDigits["ff",16]. This of course allows to use it with Map with something like, for example, FromDigits[#,16]&/@{"aa","ff"}, which is way better than using ToExpression. Thanks a lot for the pointer, I'll accept that as an answer if you care to make it such (I still wonder on why exactly is there no functional FullForm for the base^^number syntax though) – glS Jan 12 '16 at 19:29
  • @IPoiler BaseForm[#,10]& does nothing at all, as you can easily try. Show me how would you convert a list of binary (or hexadecimal, or whatever else really) numbers to base 10 with the BaseForm notation and I will be convinced! – glS Jan 12 '16 at 19:32
  • 2
    andre gave the solution (FromDigits). The reverse is not BaseForm but IntegerDigits. ^^ # & does not work because ^^ is not an operator but part of the number syntax, just like the decimal point is, or *^ for the exponent. – Szabolcs Jan 12 '16 at 19:58

1 Answers1

2

Here is a better solution than your workwaround :

FromDigits[#, 2] & /@ {"0", "11", "10", "0"}  

or :

FromDigits[#, 2] & /@ ToString /@ {0, 11, 10, 0}

{0, 3, 2, 0}

andre314
  • 18,474
  • 1
  • 36
  • 69