On 05/06/2011 10:25 PM, Carl Mäsak wrote:
> S02:3185-3280 does a nice job of explaining what can and cannot be
> done with the radix syntax (i.e. :2<1010> etc). I'm left with two
> questions, however:
> 
> * If :2<1010> is the way to way to "interpret" a string as a number in
> base two, giving the number 10 -- what's the way to go in the other
> direction?

Sounds like a job for sprintf of fmt.

> * What's the way to interpret a number of some *parameterized* base $r
> ? The syntax :$r<123> is legal, but doesn't have anything to do with
> base conversion.

FWIW in NQP you can cheat and reuse the internal function of the compiler:


09:24 < moritz> nqp: use NQPHLL; say(HLL::Actions::string_to_int('14', 8))
09:24 <+p6eval> nqp: OUTPUT«12␤»

I think in the end it should come down to exposing more number parsing
primitives as functions or methods.

On a related note, we had this discussion about a generic string parser
that would parse any Perl 6 literals, but I don't know if it ever got
specced.

If such a function exists, you could call literal(":{$base}<123>"), and
have no fear of code injection like you'd have to have with eval.

Cheers,
Moritz

Reply via email to