Skip to content
Browse files

initial commit

  • Loading branch information...
0 parents commit 196479a6e3e25483e528a214fbd0f78774f2dbe9 @cyx cyx committed Mar 27, 2010
Showing with 57 additions and 0 deletions.
  1. +11 −0 README.markdown
  2. +46 −0 base62.rb
11 README.markdown
@@ -0,0 +1,11 @@
+Usage
+=====
+
+ Base62.encode(1)
+
+ Base62.decode('lYGhA16ahyf')
+
+Running Tests
+=============
+
+ruby base62.rb
46 base62.rb
@@ -0,0 +1,46 @@
+module Base62
+ SIXTYTWO = ('0'..'9').to_a + ('a'..'z').to_a + ('A'..'Z').to_a
+
+ def encode( numeric )
+ raise ArgumentError unless Numeric === numeric
+
+ return '0' if numeric == 0
+ s = ''
+
+ while numeric > 0
+ s << Base62::SIXTYTWO[numeric.modulo(62)]
+ numeric /= 62
+ end
+ s.reverse
+ end
+
+ def decode( base62 )
+ s = base62.to_s.reverse.split('')
+
+ total = 0
+ s.each_with_index do |char, index|
+ if ord = SIXTYTWO.index(char)
+ total += ord * (62 ** index)
+ else
+ raise ArgumentError, "#{base62} has #{char} which is not valid"
+ end
+ end
+ total.to_s
+ end
+
+ module_function :encode, :decode
+end
+
+if $0 == __FILE__
+ require 'test/unit'
+
+ class Base62Test < Test::Unit::TestCase
+ def test_base_62_of_max_big_int
+ assert_equal 'lYGhA16ahyf', Base62.encode(18446744073709551615)
+ end
+
+ def test_numeric_of_base62d_max
+ assert_equal '18446744073709551615', Base62.decode('lYGhA16ahyf')
+ end
+ end
+end

0 comments on commit 196479a

Please sign in to comment.
Something went wrong with that request. Please try again.