base_convert 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.txt +84 -0
  3. data/lib/base_convert.rb +58 -0
  4. metadata +45 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dbccf02603e1f029d1acfa79abfff94b93f315c6
4
+ data.tar.gz: 44227572331e4c2203d3012f32377ea4bde2e573
5
+ SHA512:
6
+ metadata.gz: 07e12a56d9fd457e70417ab65124d4b9fba0c45ecd922ec6ebdf8ec3856da9d10be56f69d11cf364dd3e9aeb4a57d150824cda3d5383df874115af2d4400b4ac
7
+ data.tar.gz: 89dd3ea459891e0bc478b1f808e680e1b6d761c4b4bfd4e7e7e0e6440f889139de4383e503510fd23ba817eee6bbe7cf42abddf753cf270278773a099fc4fad8
@@ -0,0 +1,84 @@
1
+ # BaseConvert
2
+ # DESCRIPTION:
3
+ # Got the code originally from
4
+ # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
5
+ # which I then converted to a class.
6
+ # SINOPSIS:
7
+
8
+ def ok(a,b)
9
+ raise "bad! /#{a}/ == /#{b}/" unless a == b
10
+ end
11
+
12
+ # ruby -I ./lib README.txt
13
+ if RUBY_VERSION =~ /^1.9.2/ then
14
+ require 'base_convert'
15
+
16
+ # Verify it works up to base 36 with number.to_s(n)
17
+ 2.upto(36) do |n|
18
+ base = BaseConvert.new(n)
19
+ 100.times do
20
+ number = rand(10000)
21
+ string = number.to_s(n)
22
+ ok( base.convert(string), number )
23
+ ok( base.convert(number), string.upcase ) # BaseConvert uses caps
24
+ end
25
+ end
26
+
27
+ # BaseConvert is case insensitive up to base 36, but
28
+ # returns string values with capital leters.
29
+ # At base 37 and up, it's case sensitive with lower case letter being of higher order.
30
+ # It primary use is to reduce digest strings.
31
+ # For example:
32
+ require 'digest'
33
+ # This hexadecimal:
34
+ ok( "7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069",
35
+ hexdigest = Digest::SHA256.hexdigest('Hello World!') )
36
+ # Translatates to this word(\w except '_') string
37
+ ok( string = 'UEjKBW7lvEq1KjyCxAYQRJqmvffQEbwLNvzs8Ggdy4P',
38
+ BaseConvert.new(:hexadecimal,:word).convert(hexdigest) )
39
+ # and we can covert it back:
40
+ ok( BaseConvert.new(:word,:hexadecimal).convert(string),
41
+ hexdigest.upcase )
42
+ # Again, note the BaseConvert returns the hexdigest string capitalized.
43
+ #There is also a quotable string([[:graph:]] except [`'"]):
44
+ ok( qstring = '$<c6PCX_mugC58xfO5JOp+V4|<jHekI^_WE$?d?9',
45
+ BaseConvert.new(:hexadecimal,:qgraph).convert(hexdigest))
46
+ # And you can convert it back
47
+ ok( BaseConvert.new(:qgraph,:hexadecimal).convert(qstring),
48
+ hexdigest.upcase)
49
+ # Note the length
50
+ ok( 64, hexdigest.length)
51
+ ok( 43, string.length)
52
+ ok( 40, qstring.length)
53
+
54
+ # BaseConvert has the following primary form:
55
+ # # converting from basefrom to baseto
56
+ # base = BaseConvert.new( basefrom, baseto )
57
+ # # covert basefrom_string in basefrom to baseto_string in baseto
58
+ # baseto_string = base.convert( basefrom_string )
59
+ # # convert basefrom_string to it's decimal number representation
60
+ # decimal_number = base.base2dec( basefrom_string )
61
+ # # convert a decimal_number to it's baseto string representation
62
+ # baseto_string = base.dec2base( decimal_number )
63
+ # BaseConvert has the following heuristic forms:
64
+ # # converting to and from _base_
65
+ # base = BaseConvert.new( _base_ )
66
+ # base_string = base.convert( decimal_number )
67
+ # decimal_number = base.convert( base_string )
68
+ # BaseConvert has the following mneumonic keys that can be use for base:
69
+ # :binary => 2
70
+ # :oct,:octal => 8,
71
+ # :dec,:decimal => 10,
72
+ # :hex,:hexadecimal => 16
73
+ # BaseConvert's base can go up to 62 with :word(\w except '_') and 91 with :qgraph([:graph] except [`'"].
74
+ # By default, :word is used...
75
+ ok( '6laZE', BaseConvert.new(62).convert( 100_000_000 ))
76
+ # ...until a transition to :qgraph is required.
77
+ ok( ')8]3H', BaseConvert.new(63).convert( 100_000_000 ))
78
+ # But you can specify the digit dictionary. At this point you've read and know the code...
79
+ base = BaseConvert.new(62)
80
+ base.to_digits = BaseConvert::QGRAPH
81
+ ok( ')RGF1', base.convert( 100_000_000 ))
82
+ end
83
+ # The End.
84
+ # <i>carlosjhr64</i>@<i>gmail<i/>.<i>com</i>
@@ -0,0 +1,58 @@
1
+ # http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
2
+ class BaseConvert
3
+ DIGITS = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/ && c=~/[^_]/} # 0..9 a..z A..Z
4
+ QGRAPH = 0.upto(255).map{|i| i.chr}.select{|c| c=~/[[:graph:]]/ && c=~/[^`'"]/}
5
+
6
+ BASE = {
7
+ :word => DIGITS.length,
8
+ :qgraph => QGRAPH.length,
9
+ :hexadecimal => 16,
10
+ :hex => 16,
11
+ :decimal => 10,
12
+ :dec => 10,
13
+ :octal => 8,
14
+ :oct => 8,
15
+ :binary => 2,
16
+ }
17
+
18
+ attr_accessor :from_digits, :to_digits
19
+ def initialize(basefrom,baseto=nil)
20
+ baseto = basefrom if baseto.nil?
21
+ @basefrom = BASE[basefrom] || basefrom
22
+ @baseto = BASE[baseto] || baseto
23
+ dl = DIGITS.length
24
+ @from_digits = ((basefrom == :qgraph) || (@basefrom > dl))? QGRAPH : DIGITS
25
+ @to_digits = ((baseto == :qgraph) || (@baseto > dl))? QGRAPH : DIGITS
26
+ end
27
+
28
+ def convert(str)
29
+ if str.class == String
30
+ dec = base2dec(str)
31
+ return (@baseto == @basefrom)? dec : dec2base(dec)
32
+ end
33
+ dec2base(str)
34
+ end
35
+
36
+ def base2dec(str)
37
+ str = str.upcase if @basefrom < 37
38
+ raise ArgumentError, "base is invalid" unless @basefrom.between?(2, @from_digits.length)
39
+ res = 0
40
+ str.to_s.each_char do |c|
41
+ idx = @from_digits[0,@basefrom].find_index(c)
42
+ idx.nil? and raise ArgumentError, "invalid base-#{@basefrom} digit: #{c}"
43
+ res = res * @basefrom + idx
44
+ end
45
+ res
46
+ end
47
+
48
+ def dec2base(n)
49
+ return @to_digits.first if n == 0
50
+ raise ArgumentError, "base is invalid" unless @baseto.between?(2, @to_digits.length)
51
+ res = []
52
+ while n > 0
53
+ n, r = n.divmod(@baseto)
54
+ res.unshift(@to_digits[r])
55
+ end
56
+ res.join("")
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: base_convert
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - carlosjhr64@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-12 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Got the code originally from http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
14
+ which I then converted to a class.
15
+ email: carlosjhr64@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - "./README.txt"
21
+ - "./lib/base_convert.rb"
22
+ homepage: https://github.com/carlosjhr64/base_convert
23
+ licenses: []
24
+ metadata: {}
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ required_rubygems_version: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ requirements: []
40
+ rubyforge_project:
41
+ rubygems_version: 2.2.0
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: BaseConvert
45
+ test_files: []