Another stupid question

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

Another stupid question

Torsten Bergmann
This was just an example to play with since you asked
for a #convertFromBase:to: method in your original mail
and one cant provide such a method on Number. Your
original request

  11 convertFromBase: 2 to: 10

may work - but it wont work for hex values:

  FF convertFromBase: 16 to: 10

then FF would have to be a string.

  'FF' convertFromBase: 16 to: 10

So having a #convertFromBase:to: on String is not to recommend
(especially since the string has to be in a number format)
and also not necessary. I should have added this to my mail
as a warning.

Just use

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

If necessary put it in a private helper method for conversion.

May I ask why you require conversions between the different base systems?
Working on a hex editor for Nautilus? ;)

Bye
T.



Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Benjamin Van Ryseghem (Pharo)
On Feb 15, 2013, at 1:40 AM, "Torsten Bergmann" <[hidden email]> wrote:

> This was just an example to play with since you asked
> for a #convertFromBase:to: method in your original mail
> and one cant provide such a method on Number. Your
> original request
>
>  11 convertFromBase: 2 to: 10
>
> may work - but it wont work for hex values:
>
>  FF convertFromBase: 16 to: 10
>
> then FF would have to be a string.
>
>  'FF' convertFromBase: 16 to: 10
>
> So having a #convertFromBase:to: on String is not to recommend
> (especially since the string has to be in a number format)
> and also not necessary. I should have added this to my mail
> as a warning.
>
> Just use
>
>   (Number readFrom: self base: sourceBase) printStringBase: targetBase
>

Thank you

> If necessary put it in a private helper method for conversion.
>
> May I ask why you require conversions between the different base systems?

I have to implement a FTP server for a lecture, and the port is provided in a strange format (twice 8 bits).
So I needed to change the port in base 10 to base 2, cut it into two pieces, then turn those pieces back in base 10 :)

> Working on a hex editor for Nautilus? ;)

Is a hex editor a need ? :)

Ben

>
> Bye
> T.
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Henrik Sperre Johansen

On Feb 15, 2013, at 1:51 AM, Benjamin wrote:

> On Feb 15, 2013, at 1:40 AM, "Torsten Bergmann" <[hidden email]> wrote:
>
>> This was just an example to play with since you asked
>> for a #convertFromBase:to: method in your original mail
>> and one cant provide such a method on Number. Your
>> original request
>>
>> 11 convertFromBase: 2 to: 10
>>
>> may work - but it wont work for hex values:
>>
>> FF convertFromBase: 16 to: 10
>>
>> then FF would have to be a string.
>>
>> 'FF' convertFromBase: 16 to: 10
>>
>> So having a #convertFromBase:to: on String is not to recommend
>> (especially since the string has to be in a number format)
>> and also not necessary. I should have added this to my mail
>> as a warning.
>>
>> Just use
>>
>>  (Number readFrom: self base: sourceBase) printStringBase: targetBase
>>
>
> Thank you
>
>> If necessary put it in a private helper method for conversion.
>>
>> May I ask why you require conversions between the different base systems?
>
> I have to implement a FTP server for a lecture, and the port is provided in a strange format (twice 8 bits).
> So I needed to change the port in base 10 to base 2, cut it into two pieces, then turn those pieces back in base 10 :)
>
>> Working on a hex editor for Nautilus? ;)
>
> Is a hex editor a need ? :)
>
> Ben
>
>>
>> Bye
>> T.
>>
>>
>>
>
>
Wait, wait, wait.

You mean like #[16 45] ?

What's wrong with
Number1 := sourceNumber >> 8
Number2 := sourceNumber bitAnd: 255

?

Cheers,
Henry
Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Sven Van Caekenberghe-2

On 15 Feb 2013, at 11:11, Henrik Johansen <[hidden email]> wrote:

> On Feb 15, 2013, at 1:51 AM, Benjamin wrote:
>
>> On Feb 15, 2013, at 1:40 AM, "Torsten Bergmann" <[hidden email]> wrote:
>>
>>> This was just an example to play with since you asked
>>> for a #convertFromBase:to: method in your original mail
>>> and one cant provide such a method on Number. Your
>>> original request
>>>
>>> 11 convertFromBase: 2 to: 10
>>>
>>> may work - but it wont work for hex values:
>>>
>>> FF convertFromBase: 16 to: 10
>>>
>>> then FF would have to be a string.
>>>
>>> 'FF' convertFromBase: 16 to: 10
>>>
>>> So having a #convertFromBase:to: on String is not to recommend
>>> (especially since the string has to be in a number format)
>>> and also not necessary. I should have added this to my mail
>>> as a warning.
>>>
>>> Just use
>>>
>>> (Number readFrom: self base: sourceBase) printStringBase: targetBase
>>>
>>
>> Thank you
>>
>>> If necessary put it in a private helper method for conversion.
>>>
>>> May I ask why you require conversions between the different base systems?
>>
>> I have to implement a FTP server for a lecture, and the port is provided in a strange format (twice 8 bits).
>> So I needed to change the port in base 10 to base 2, cut it into two pieces, then turn those pieces back in base 10 :)
>>
>>> Working on a hex editor for Nautilus? ;)
>>
>> Is a hex editor a need ? :)
>>
>> Ben
>>
>>>
>>> Bye
>>> T.
>>>
>>>
>>>
>>
>>
> Wait, wait, wait.
>
> You mean like #[16 45] ?
>
> What's wrong with
> Number1 := sourceNumber >> 8
> Number2 := sourceNumber bitAnd: 255
>
> ?
>
> Cheers,
> Henry

There is also

  sourceNumber asByteArrayOfSize: 2

  1000 asByteArrayOfSize:2 #[3 232]

Sven
Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Benjamin Van Ryseghem (Pharo)
On Feb 15, 2013, at 11:24 AM, Sven Van Caekenberghe <[hidden email]> wrote:

>
> On 15 Feb 2013, at 11:11, Henrik Johansen <[hidden email]> wrote:
>
>> On Feb 15, 2013, at 1:51 AM, Benjamin wrote:
>>
>>> On Feb 15, 2013, at 1:40 AM, "Torsten Bergmann" <[hidden email]> wrote:
>>>
>>>> This was just an example to play with since you asked
>>>> for a #convertFromBase:to: method in your original mail
>>>> and one cant provide such a method on Number. Your
>>>> original request
>>>>
>>>> 11 convertFromBase: 2 to: 10
>>>>
>>>> may work - but it wont work for hex values:
>>>>
>>>> FF convertFromBase: 16 to: 10
>>>>
>>>> then FF would have to be a string.
>>>>
>>>> 'FF' convertFromBase: 16 to: 10
>>>>
>>>> So having a #convertFromBase:to: on String is not to recommend
>>>> (especially since the string has to be in a number format)
>>>> and also not necessary. I should have added this to my mail
>>>> as a warning.
>>>>
>>>> Just use
>>>>
>>>> (Number readFrom: self base: sourceBase) printStringBase: targetBase
>>>>
>>>
>>> Thank you
>>>
>>>> If necessary put it in a private helper method for conversion.
>>>>
>>>> May I ask why you require conversions between the different base systems?
>>>
>>> I have to implement a FTP server for a lecture, and the port is provided in a strange format (twice 8 bits).
>>> So I needed to change the port in base 10 to base 2, cut it into two pieces, then turn those pieces back in base 10 :)
>>>
>>>> Working on a hex editor for Nautilus? ;)
>>>
>>> Is a hex editor a need ? :)
>>>
>>> Ben
>>>
>>>>
>>>> Bye
>>>> T.
>>>>
>>>>
>>>>
>>>
>>>
>> Wait, wait, wait.
>>
>> You mean like #[16 45] ?
>>
>> What's wrong with
>> Number1 := sourceNumber >> 8
>> Number2 := sourceNumber bitAnd: 255
>>
>> ?
>>
>> Cheers,
>> Henry
>
> There is also
>
>  sourceNumber asByteArrayOfSize: 2
>
>  1000 asByteArrayOfSize:2 #[3 232]

Thank you guys :)

I definitely feel stupid now :D

Ben

>
> Sven


Reply | Threaded
Open this post in threaded view
|

Re: Another stupid question

Henrik Sperre Johansen
On 15.02.2013 11:29, Benjamin wrote:

> On Feb 15, 2013, at 11:24 AM, Sven Van Caekenberghe <[hidden email]> wrote:
>
>> On 15 Feb 2013, at 11:11, Henrik Johansen <[hidden email]> wrote:
>>
>>> On Feb 15, 2013, at 1:51 AM, Benjamin wrote:
>>>
>>>> On Feb 15, 2013, at 1:40 AM, "Torsten Bergmann" <[hidden email]> wrote:
>>>>
>>>>> This was just an example to play with since you asked
>>>>> for a #convertFromBase:to: method in your original mail
>>>>> and one cant provide such a method on Number. Your
>>>>> original request
>>>>>
>>>>> 11 convertFromBase: 2 to: 10
>>>>>
>>>>> may work - but it wont work for hex values:
>>>>>
>>>>> FF convertFromBase: 16 to: 10
>>>>>
>>>>> then FF would have to be a string.
>>>>>
>>>>> 'FF' convertFromBase: 16 to: 10
>>>>>
>>>>> So having a #convertFromBase:to: on String is not to recommend
>>>>> (especially since the string has to be in a number format)
>>>>> and also not necessary. I should have added this to my mail
>>>>> as a warning.
>>>>>
>>>>> Just use
>>>>>
>>>>> (Number readFrom: self base: sourceBase) printStringBase: targetBase
>>>>>
>>>> Thank you
>>>>
>>>>> If necessary put it in a private helper method for conversion.
>>>>>
>>>>> May I ask why you require conversions between the different base systems?
>>>> I have to implement a FTP server for a lecture, and the port is provided in a strange format (twice 8 bits).
>>>> So I needed to change the port in base 10 to base 2, cut it into two pieces, then turn those pieces back in base 10 :)
>>>>
>>>>> Working on a hex editor for Nautilus? ;)
>>>> Is a hex editor a need ? :)
>>>>
>>>> Ben
>>>>
>>>>> Bye
>>>>> T.
>>>>>
>>>>>
>>>>>
>>>>
>>> Wait, wait, wait.
>>>
>>> You mean like #[16 45] ?
>>>
>>> What's wrong with
>>> Number1 := sourceNumber >> 8
>>> Number2 := sourceNumber bitAnd: 255
>>>
>>> ?
>>>
>>> Cheers,
>>> Henry
>> There is also
>>
>>   sourceNumber asByteArrayOfSize: 2
>>
>>   1000 asByteArrayOfSize:2 #[3 232]
> Thank you guys :)
>
> I definitely feel stupid now :D
>
> Ben
<a href="http://www.catb.org/esr/faqs/smart-questions.html#idp29958640http://www.catb.org/esr/faqs/smart-questions.html#idp29958640">http://www.catb.org/esr/faqs/smart-questions.html#idp29958640http://www.catb.org/esr/faqs/smart-questions.html#idp29958640

;)

Cheers,
Henry