Another stupid question

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Another stupid question

Benjamin Van Ryseghem (Pharo)
Hi guys, I know it sounds stupid but how do I convert numbers from one base to another ?

I found 

3 printStringBase: 2 -> '11'

But I would like something like

11 convertFromBase: 2 to: 10
-> 3

The Finder did help me about this one so I ask here :)

Thanks in advance,

Ben

Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

jannik laval
Hi Ben,

use that:

2r11 printStringBase: 10
-> 3

16r11 printStringBase: 10
->17

Cheers,
Jannik

On Feb 14, 2013, at 10:05 PM, Benjamin <[hidden email]> wrote:

Hi guys, I know it sounds stupid but how do I convert numbers from one base to another ?

I found 

3 printStringBase: 2 -> '11'

But I would like something like

11 convertFromBase: 2 to: 10
-> 3

The Finder did help me about this one so I ask here :)

Thanks in advance,

Ben


Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Sven Van Caekenberghe-2
In reply to this post by Benjamin Van Ryseghem (Pharo)

On 14 Feb 2013, at 22:05, Benjamin <[hidden email]> wrote:

> Hi guys, I know it sounds stupid but how do I convert numbers from one base to another ?
>
> I found
>
> 3 printStringBase: 2 -> '11'
>
> But I would like something like
>
> 11 convertFromBase: 2 to: 10
> -> 3
>
> The Finder did help me about this one so I ask here :)
>
> Thanks in advance,
>
> Ben

It is not the number itself that is in some base, but its representation.
So you have to convert from one string representation to another.

So something like this should work:

  (Integer readFrom: 'FF' base: 16) printStringBase: 2

Sven

--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill