Return to Snippet

Revision: 1969
at December 6, 2006 15:48 by gusto


Initial Code
DIGITS = ('0'..'9').to_a + ('A'..'Z').to_a

def to_base36_from_l(num)
	result = ""
	while num != 0
		tmp = num / DIGITS.length
		remainder = num - tmp * DIGITS.length
		char = DIGITS[remainder]
		num = tmp
		result = char + result
	end
	result
end

def to_l_from_base36(str)
	answer = 0
	str.reverse.split("").each_with_index { |c, i|
		answer += (DIGITS.index(c) * (DIGITS.length ** i))
	}
	answer
end

# sample usage

num = 1451252361345125

base36 = to_base36_from_l(num)
puts "#{base36} from #{to_l_from_base36(base36)}"

# output: EAFC4PPW85 from 1451252361345125

Initial URL

                                

Initial Description

                                

Initial Title
Decimal to Base 36 / Base 36 to Decimal

Initial Tags

                                

Initial Language
Other