RubyDubee
RubyDubee

Reputation: 2446

Interconversion between decimal and any other base-n number system

I have written some general functions to convert between decimal and any other base-n number system(n<=36 for now) and vice-versa. Don't want to make things messy here so i have posted the code here.

Could anybody suggest any better way for this? May be more effective and Rubyish?

Thanks

Upvotes: 7

Views: 1954

Answers (2)

glenn jackman
glenn jackman

Reputation: 246877

Check out Rosetta Code: http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

There's already the to_s method on Numeric and the to_i method on String to convert back:

irb(main):013:0> 10.to_s(36)
=> "a"
irb(main):014:0> "a".to_i(36)
=> 10

Upvotes: 13

Related Questions