/ Published in: Other

Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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
Comments
