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

Torsten Bergmann
Add this method to String class:

convertFromBase: sourceBase to: targetBase
        "Convert the receiver (who has to represent a number string) from the given source base to
          the given target base
               
                'FFFF' convertFromBase: 16 to: 2
        "
        ^(sourceBase asString,'r', self) asNumber printStringBase: targetBase




And then try:

  'FFFF' convertFromBase: 16 to: 10
  'FFFF' convertFromBase: 16 to: 2
  '11111110' convertFromBase: 2 to: 16
  ...

Have fun
Torsten

Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Sven Van Caekenberghe-2

On 15 Feb 2013, at 00:09, "Torsten Bergmann" <[hidden email]> wrote:

> convertFromBase: sourceBase to: targetBase
> "Convert the receiver (who has to represent a number string) from the given source base to
>  the given target base
>
> 'FFFF' convertFromBase: 16 to: 2
> "
> ^(sourceBase asString,'r', self) asNumber printStringBase: targetBase

Please, make that

        ^ (Number readFrom: self base: sourceBase) printStringBase: targetBase

No useless copying and concatenating, simpler and more elegant.

Sven

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


Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

stephane ducasse
so could you write two tests and submit a slice so that we improve the system

Stef

>> convertFromBase: sourceBase to: targetBase
>> "Convert the receiver (who has to represent a number string) from the given source base to
>>  the given target base
>>
>> 'FFFF' convertFromBase: 16 to: 2
>> "
>> ^(sourceBase asString,'r', self) asNumber printStringBase: targetBase
>
> Please, make that
>
> ^ (Number readFrom: self base: sourceBase) printStringBase: targetBase
>
> No useless copying and concatenating, simpler and more elegant.
>
> Sven
>
> --
> Sven Van Caekenberghe
> http://stfx.eu
> Smalltalk is the Red Pill
>
>