[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Base 26

Stefan Rusterholz

7/13/2007 8:37:00 PM

Mike Moore wrote:
> Hey friends! What is the best way to convert a Numeric to a string
> encoded
> with base 26? I want the string to contain just alpha characters, not
> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
> 'a-z')
> and I'm wondering if there is a more concise way.
>

You're looking too far away.
int.to_s(26)

Regards
Stefan

--
Posted via http://www.ruby-....

6 Answers

Stefan Rusterholz

7/13/2007 8:39:00 PM

0

Stefan Rusterholz wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
>>
>
> You're looking too far away.
> int.to_s(26)
>
> Regards
> Stefan

OK, I should read before answering :)
Why are you using a-z instead of 0-9a-p?

Anyway, if you need that instead of what to_s(26) already does, then I
don't know a shorter way.

Regards
Stefan

--
Posted via http://www.ruby-....

Phrogz

7/13/2007 8:51:00 PM

0

On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.
> Anyway, if you need that instead of what to_s(26) already does, then I
> don't know a shorter way.

How about (i+10).to_s(36)

Warren Brown

7/13/2007 9:05:00 PM

0

>>> Hey friends! What is the best way to convert a
>>> Numeric to a string encoded with base 26? I want
>>> the string to contain just alpha characters, not
>>> alphanumeric characters. For now I'm using
>>> num.to_s(26).tr('0-9a-p','a-z')
>>> and I'm wondering if there is a more concise way.

>> Anyway, if you need that instead of what to_s(26)
>> already does, then I don't know a shorter way.

> How about (i+10).to_s(36)

You mean like:

irb(main):001:0> i = 42
=> 42
irb(main):002:0> (i+10).to_s(26)
=> "20"

Um, no, I don't think that fits the bill :o)

- Warren Brown


Robert Dober

7/14/2007 7:46:00 AM

0

On 7/13/07, Stefan Rusterholz <apeiros@gmx.net> wrote:
> Stefan Rusterholz wrote:
> > Mike Moore wrote:
> >> Hey friends! What is the best way to convert a Numeric to a string
> >> encoded
> >> with base 26? I want the string to contain just alpha characters, not
> >> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
> >> 'a-z')
> >> and I'm wondering if there is a more concise way.
> >>
> >
> > You're looking too far away.
> > int.to_s(26)
> >
> > Regards
> > Stefan
>
> OK, I should read before answering :)
Honestly no, it was quite funny :)
Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Robert Dober

7/14/2007 7:47:00 AM

0

On 7/13/07, Phrogz <phrogz@mac.com> wrote:
> On Jul 13, 2:39 pm, Stefan Rusterholz <apei...@gmx.net> wrote:
> > Mike Moore wrote:
> >> Hey friends! What is the best way to convert a Numeric to a string
> >> encoded
> >> with base 26? I want the string to contain just alpha characters, not
> >> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
> >> 'a-z')
> >> and I'm wondering if there is a more concise way.
> > Anyway, if you need that instead of what to_s(26) already does, then I
> > don't know a shorter way.
>
> How about (i+10).to_s(36)
>
What is this thread, first Stefan now you?
Robert
--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Robert Klemme

7/14/2007 11:22:00 AM

0

On 13.07.2007 22:37, Stefan Rusterholz wrote:
> Mike Moore wrote:
>> Hey friends! What is the best way to convert a Numeric to a string
>> encoded
>> with base 26? I want the string to contain just alpha characters, not
>> alphanumeric characters. For now I'm using num.to_s(26).tr('0-9a-p',
>> 'a-z')
>> and I'm wondering if there is a more concise way.

I think your solution is pretty concise already. You could however do
something like this:

class Integer
C26 = (?a..?z).to_a.freeze

def to_b26
x = self
s = ""
until x == 0
m = x % 26
s << C26[m]
x = (x - m) / 26
end
s == "" ? "0" : s.reverse!
end
end

Or, a more generic solution:

class Integer
def to_base(chars)
raise ArgumentError if chars.empty?
return chars[0].chr if self == 0
x = self
s = ""
until x == 0
m = x % chars.size
s << chars[m]
x = (x - m) / chars.size
end
s.reverse!
end
end

Now you can do

irb(main):065:0> 1.to_base( (?a..?z).to_a )
=> "b"
irb(main):066:0> 26.to_base( (?a..?z).to_a )
=> "ba"

And you can even use a String as argument - duck typing at its best!

irb(main):067:0> 7.to_base "01"
=> "111"
irb(main):068:0> 6.to_base "01"
=> "110"

Kind regards

robert