Skip to content

Instantly share code, notes, and snippets.

@danielnc
Created October 25, 2013 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielnc/7161732 to your computer and use it in GitHub Desktop.
Save danielnc/7161732 to your computer and use it in GitHub Desktop.
Stupid and un-optimized converter to other numerical bases(NUMBER.to_s(base))
def convert(number, base=10)
raise "base must be between 2 and 36" if base < 2 || base > 36
negative = number < 0
number = number.abs
number_array = []
loop do
number_array.unshift("0123456789abcdefghijklmnopqrstuvwxyz"[number % base])
number /= base
break if number == 0
end
number_array.unshift("-") if negative
number_array.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment