Base 26

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.

Thanks,
~Mike

http://blowmage.com/

http://mtnwestruby.org/

Stefan R. wrote:

Mike M. 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 :slight_smile:
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

Mike M. 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

On Jul 13, 2:39 pm, Stefan R. [email protected] wrote:

Mike M. 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)

On 7/13/07, Stefan R. [email protected] wrote:

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

Regards
Stefan

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

Because it is one extra step to switch to the number keys on the iPhone
keyboard. :slight_smile:

Anyway, if you need that instead of what to_s(26) already does, then I

don’t know a shorter way.

Yeah, I was looking for an offset value, something like
Numeric#to_s(base=10, offset=0)

Regards

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 B.

On 7/13/07, Phrogz [email protected] wrote:

don’t know a shorter way.

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

If it ends up in base 36 then it’s still going to get numbers in it,
just
not for the first ten numbers! :slight_smile:

To the original poster… no, it looks like you’re doing it the best way
IMHO. What you are trying to do is not standard (that is, base 26 is
0-9a-p), so a single ‘tr’ is nothng to sniff at to get your special
variant
:slight_smile: Just abstract it away into your own method on Fixnum/Numeric if you
want
to keep it lean in your other code.

Cheers,
Peter C.

On 7/13/07, Phrogz [email protected] wrote:

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

Unfortunately that doesn’t work. :slight_smile:

9.to_s(26).tr(‘0-9a-p’, ‘a-z’) => “j”
(9+10).to_s(26).tr(‘0-9a-p’, ‘a-z’) => “t”

99.to_s(26).tr(‘0-9a-p’, ‘a-z’) => “dv”
(99+10).to_s(26).tr(‘0-9a-p’, ‘a-z’) => “ef”

999.to_s(26).tr(‘0-9a-p’, ‘a-z’) => “bml”
(999+10).to_s(26).tr(‘0-9a-p’, ‘a-z’) => “bmv”

9999.to_s(26).tr(‘0-9a-p’, ‘a-z’) => “oup”
(9999+10).to_s(26).tr(‘0-9a-p’, ‘a-z’) => “ouz”

For more info see Wikipedia:

On 7/13/07, Peter C. [email protected] wrote:

‘a-z’)
To the original poster… no, it looks like you’re doing it the best way
IMHO. What you are trying to do is not standard (that is, base 26 is
0-9a-p), so a single ‘tr’ is nothng to sniff at to get your special
variant
:slight_smile: Just abstract it away into your own method on Fixnum/Numeric if you
want
to keep it lean in your other code.

Thanks Peter. I didn’t mean to sniff at a single ‘tr’, I was just
hoping
for a better way. Right now I have my ‘to_base26’ and ‘from_base26’
methods
on Numeric and String respectively, but I think I might monkey-patch
‘to_s’
and ‘to_i’ and add the base 26 logic when the ‘base’ argument is 26.

Cheers,

On 7/13/07, Mike M. [email protected] wrote:

Thanks Peter. I didn’t mean to sniff at a single ‘tr’, I was just hoping
for a better way. Right now I have my ‘to_base26’ and ‘from_base26’
methods
on Numeric and String respectively, but I think I might monkey-patch
‘to_s’
and ‘to_i’ and add the base 26 logic when the ‘base’ argument is 26.

You can do it whichever way you want, of course, but in terms of good
coding
practices I’d advise not monkey patching to_s and to_i in this way as it
could break expected behavior (for libraries you use, etc). Monkey
patching
is okay to add features, but /changing/ existing features can get messy.
Unless, of course, you add an optional param and detect that instead
(e.g.
to_s(base, start=‘0’) … then use .to_s(26, ‘a’) for yourself), but by
that
point it’s possibly easier just to make your own methods.

Cheers,
Peter C.

On 7/13/07, Phrogz [email protected] wrote:

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

What is this thread, first Stefan now you?
Robert

On 13.07.2007 22:37, Stefan R. wrote:

Mike M. 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

On 7/13/07, Stefan R. [email protected] wrote:

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

Regards
Stefan

OK, I should read before answering :slight_smile:
Honestly no, it was quite funny :slight_smile:
Robert

Hi,

Am Samstag, 14. Jul 2007, 05:20:46 +0900 schrieb Mike M.:

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.

Isn’t that concise enough? It’s one line, it’s
comprehensible right away and it reflects higher logic.
So what do you want more?

Compare it to this:

class Fixnum
def to_a_z
n, r = abs, “”
begin
n, d = n.divmod 26
r.insert 0, (d+?a).chr
end while n > 0
r.insert 0, “-” if self < 0
r
end
end

Bertram