Base62 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/Base62.rb +40 -0
  2. metadata +45 -0
@@ -0,0 +1,40 @@
1
+ class Base62
2
+
3
+ @@base = 62
4
+ @@dictionary = nil
5
+
6
+ def self.to_base62(n_base10)
7
+ init_dictionary if @@dictionary.nil?
8
+ result = ''
9
+ while n_base10 > (@@base - 1)
10
+ place = n_base10 % @@base
11
+ n_base10 /= @@base
12
+ result = @@dictionary[place] + result
13
+ end
14
+ @@dictionary[n_base10] + result
15
+ end
16
+
17
+ def self.to_base10(n_base62)
18
+ init_dictionary if @@dictionary.nil?
19
+ result = 0
20
+ exp = n_base62.size - 1
21
+ n_base62.each_byte do |c|
22
+ curIndex = @@dictionary.index(c.chr)
23
+ result += curIndex * (@@base ** exp)
24
+ exp -= 1
25
+ end
26
+ result
27
+ end
28
+
29
+ def self.init_dictionary
30
+ @@dictionary = [('0'..'9'),
31
+ ('a'..'z'),
32
+ ('A'..'Z')].map{|r| r.to_a}.flatten
33
+ end
34
+ end
35
+
36
+
37
+
38
+
39
+
40
+
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Base62
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jorge Arrese Rodriguez
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A simple base62 converter
15
+ email: jorge.arrese@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/Base62.rb
21
+ homepage: ''
22
+ licenses: []
23
+ post_install_message:
24
+ rdoc_options: []
25
+ require_paths:
26
+ - lib
27
+ required_ruby_version: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 1.8.24
42
+ signing_key:
43
+ specification_version: 3
44
+ summary: Base62 converter
45
+ test_files: []