base_convert 2.2.0 → 3.0.191214
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +97 -99
- data/lib/base_convert.rb +27 -6
- data/lib/base_convert/configuration.rb +24 -2
- data/lib/base_convert/from_to.rb +18 -26
- data/lib/base_convert/number.rb +133 -20
- metadata +4 -7
- data/lib/base_convert/functions.rb +0 -25
- data/lib/base_convert/helpers.rb +0 -37
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 143b58ce952648fedb71d60c1b428438820b9b39b47c2cb58a54efa06695b6f3
|
4
|
+
data.tar.gz: 78917a3080a0cdb82dfd2cc626e958b7f988b38da047dba56ca7ba306279238b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bb733ffdba99c28b82cefca3150f7c08768b1e11f0385cdcfc8be9e9f2865546baf956a97b9da9fa4685599ab449990ccee485414f22f81580ba2ef6049807ee
|
7
|
+
data.tar.gz: 95d9a9dc8764a6ce432facbc7917760ee2d94df31ea066408ec6c45b978abd5e3a6cfefd3a43ca193f05ea2dbea802814f95ad6b6701c5e4001acce6122abe7e
|
data/README.md
CHANGED
@@ -17,139 +17,137 @@ See also rosettacode.org's [Non-decimal radices convert](http://rosettacode.org/
|
|
17
17
|
## SYNOPSIS:
|
18
18
|
|
19
19
|
require 'base_convert'
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
|
21
|
+
#toi string, base, digits #=> integer
|
22
|
+
BaseConvert.toi 'FF', 16, '0123456789ABCDEF' #=> 255
|
23
|
+
|
24
|
+
#tob integer, base, digits #=> string
|
25
|
+
BaseConvert.tob 255, 16, '0123456789ABCDEF' #=> "FF"
|
26
|
+
|
27
|
+
# FromTo
|
28
|
+
c = BaseConvert::FromTo.new base: 16, digits: '0123456789ABCDEF', to_base: 7, to_digits: 'abcdefg'
|
29
|
+
c['FFF'] #=> "begea"
|
30
|
+
|
31
|
+
# Number
|
32
|
+
n = BaseConvert::Number.new 'FF', base: 16, digits: '0123456789ABCDEF'
|
33
|
+
n.to_i #=> 255
|
34
|
+
n.to_s #=> "FF"
|
35
|
+
#
|
36
|
+
n = n.to_base 64, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
|
37
|
+
n.to_s #=> "D/"
|
38
|
+
n.to_i #=> 255
|
39
|
+
|
40
|
+
## INSTALL:
|
41
|
+
|
42
|
+
$ gem install base_convert
|
25
43
|
|
26
44
|
## BUT WAIT, THERE'S MORE:
|
27
45
|
|
28
46
|
Using `irb` to demonstrate the features.
|
29
47
|
The components are scoped under `BaseConvert`:
|
30
48
|
|
31
|
-
|
32
|
-
|
33
|
-
require 'base_convert' #=> true
|
34
|
-
include BaseConvert #=> Object
|
49
|
+
> irb
|
50
|
+
Welcome to IRB...
|
51
|
+
>> require 'base_convert' #=> true
|
52
|
+
>> include BaseConvert #=> Object
|
35
53
|
|
36
54
|
`base_convert` provides three ways to convert a string representation of a number.
|
37
55
|
The first is functional. One can extend(import) the functions that do the conversions.
|
38
|
-
The conversion functions are `
|
56
|
+
The conversion functions are `toi` and `tob`.
|
39
57
|
For example, the octal number "7777":
|
40
58
|
|
41
|
-
extend
|
59
|
+
extend BaseConvert #=> main
|
42
60
|
digits = '01234567'
|
43
61
|
base = digits.length #=> 8
|
44
|
-
|
45
|
-
|
62
|
+
toi('7777', base, digits) #=> 4095
|
63
|
+
tob(4095, base, digits) #=> "7777"
|
46
64
|
|
47
65
|
You can work with arbitrary digits:
|
48
66
|
|
49
67
|
digits = ')!@#$%^&'
|
50
68
|
base = digits.length #=> 8
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
For convenience, `base_convert` provides some predefined sets of digits.
|
55
|
-
`GRAPH` are the ASCII graph characters.
|
56
|
-
`QGRAPH` are the ASCII graph characters except quotes: double-quote, single-quote, and back-tick.
|
57
|
-
`WORD_` are the ASCII word characters including underscore(_).
|
58
|
-
`WORD` are the ASCII word characters except underscore(_).
|
59
|
-
`UNAMBIGUOUS` are the characters in `WORD` without the `AMBIGUOUS` characters(B8G6I1l0OQDS5Z2).
|
69
|
+
toi('&&&&', base, digits) #=> 4095
|
70
|
+
tob(4095, base, digits) #=> "&&&&"
|
60
71
|
|
61
|
-
|
62
|
-
Configuration::QGRAPH #=> "!#...01...AB...ab...}~"
|
63
|
-
to_base(4095, 8, Configuration::WORD) #=> "7777"
|
64
|
-
to_base(4095, 8, Configuration::QGRAPH) #=> "****"
|
72
|
+
For convenience, `base_convert` provides under `module BaseConvert::Configuration` some predefined sets of digits.
|
65
73
|
|
66
|
-
|
67
|
-
|
74
|
+
* `GRAPH` are the ASCII graph characters.
|
75
|
+
* `QGRAPH` are the ASCII graph characters except quotes: double-quote, single-quote, and back-tick.
|
76
|
+
* `BASE64` is the standard base 64 digits from people with no sense of order.
|
77
|
+
* `WORD_` are the ASCII word characters including underscore(`_`).
|
78
|
+
* `WORD` are the ASCII word characters except underscore(`_`).
|
79
|
+
* `UNAMBIGUOUS` are the characters in `WORD` without the `AMBIGUOUS` characters(B8G6I1l0OQDS5Z2).
|
68
80
|
|
69
|
-
|
70
|
-
o2h = FromTo.new(8, 16)
|
71
|
-
h2o.convert('FFFF') #=> "177777"
|
72
|
-
o2h.convert('177777') #=> "FFFF"
|
73
|
-
|
74
|
-
You can access the conversion alphabets via
|
75
|
-
the accessors `FromTo#from_digits` and `FromTo#to_digits`.
|
76
|
-
By default, WORD is used for bases upto 62.
|
77
|
-
For bigger bases upto 91, QGRAPH is used.
|
78
|
-
You can have bigger bases, but
|
79
|
-
you will need to come up with a bigger alphabet, perhaps
|
80
|
-
by adding greek letters.
|
81
|
-
Note that when there's no ambiguity while using WORD,
|
82
|
-
FromTo will upcase the string.
|
83
|
-
|
84
|
-
h2o.convert('FFFF') #=> "177777"
|
85
|
-
h2o.convert('ffff') #=> "177777"
|
86
|
-
|
87
|
-
FromTo also makes available the intermediary methods that works with integers,
|
88
|
-
FromTo#base2integer and FromTo#integer2Base:
|
89
|
-
|
90
|
-
h2o.base2integer('FFFF') #=> 65535
|
91
|
-
h2o.integer2base(65535) #=> "177777"
|
92
|
-
|
93
|
-
The third way to convert is via the subclass of String, `BaseConvert::Number`:
|
94
|
-
|
95
|
-
hexadecimal = Number.new('FFFF', 16, Configuration::WORD) #=> "FFFF"
|
96
|
-
# WORD is the default alphabet
|
97
|
-
hexadecimal = Number.new('FFFF', 16) #=> "FFFF"
|
98
|
-
hexadecimal.to_integer #=> 65535
|
99
|
-
# The default base is 10.
|
100
|
-
decimal = Number.new('65535') # "65535"
|
101
|
-
decimal.to_integer #=> 65535
|
102
|
-
decimal.to_base(16) #=> "FFFF"
|
103
|
-
decimal.to_base(8) #=> "177777"
|
104
|
-
# You can also specify the digits to use
|
105
|
-
digits = ')!@#$%^&'
|
106
|
-
decimal.to_base(8, digits) #=> "!&&&&&"
|
81
|
+
Some examples:
|
107
82
|
|
108
|
-
|
83
|
+
Configuration::UNAMBIGUOUS #=> "3479ACEFHJKLMNPRTUVWXYabcdefghijkmnopqrstuvwxyz"
|
84
|
+
# etc...
|
85
|
+
tob 255, 16, Configuration::WORD #=> "FF"
|
86
|
+
include Configuration
|
87
|
+
tob 255, 64, BASE64 #=> "D/"
|
109
88
|
|
110
|
-
|
89
|
+
The second way to convert is via a conversion object of `BaseConvert::FromTo`.
|
90
|
+
For example, to convert from hexadecimal to octal, and back:
|
111
91
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
unambiguous: UNAMBIGUOUS
|
117
|
-
hexadecimal: 16
|
118
|
-
hex: 16
|
119
|
-
decimal: 10
|
120
|
-
dec: 10
|
121
|
-
octal: 8
|
122
|
-
oct: 8
|
123
|
-
binary: 2
|
92
|
+
h2o = FromTo.new base: 16, to_base: 8
|
93
|
+
o2h = FromTo.new base: 8, to_base: 16
|
94
|
+
h2o['FFFF'] #=> "177777"
|
95
|
+
o2h['177777'] #=> "FFFF"
|
124
96
|
|
125
|
-
The
|
126
|
-
Examples:
|
97
|
+
The third way to work with variant base and digits numbers is via the `BaseConvert::Number`:
|
127
98
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
> BaseConvert::Number.new('$m', :qgraph).to_base(:unambiguous) #=> "CX"
|
132
|
-
> BaseConvert::Number.new('CX', :unambiguous).to_base(:oct) #=> "377"
|
133
|
-
> BaseConvert::Number.new('377', :oct).to_base(:hexadecimal) #=> "FF"
|
99
|
+
hexadecimal = Number.new('FFFF', base: 16, digits: WORD)
|
100
|
+
hexadecimal.to_s #=> "FFFF"
|
101
|
+
hexadecimal.to_i #=> 65535
|
134
102
|
|
135
|
-
|
103
|
+
# Number will infer your most likely meaning:
|
104
|
+
Number.new('FF', 16).to_i #=> 255
|
136
105
|
|
137
|
-
|
106
|
+
# And given a string of at least length 8,
|
107
|
+
# it'll go ahead and guess at your meaning:
|
108
|
+
Number.new('FFFFFFFF').to_i #=> 4294967295
|
138
109
|
|
139
|
-
|
110
|
+
# But best practice is to fully specify,
|
111
|
+
# which is easy to do with keys:
|
112
|
+
n = Number.new 'F', base: :hex, digits: :word
|
113
|
+
n.to_i #=> 15
|
114
|
+
n.to_s #=> "F"
|
140
115
|
|
141
|
-
#
|
142
|
-
|
116
|
+
# One can make a change of digits:
|
117
|
+
n = n.to_digits '0123456789abcdef'
|
118
|
+
n.to_s #=> "f"
|
119
|
+
n.to_i #=> 15
|
143
120
|
|
144
|
-
#
|
145
|
-
|
121
|
+
# One can make of change of base:
|
122
|
+
n = n.to_base 8
|
123
|
+
n.to_s #=> "17"
|
146
124
|
|
147
|
-
#
|
148
|
-
|
125
|
+
# One can make of change of base and digits:
|
126
|
+
n = n.to_base 32, :base64
|
127
|
+
# or vice-versa
|
128
|
+
n = n.to_digits :base64, 32
|
129
|
+
n.to_s #=> "P"
|
149
130
|
|
150
|
-
##
|
131
|
+
## Keys (Symbols)
|
151
132
|
|
152
|
-
|
133
|
+
Instead of giving the base number or the digits' string,
|
134
|
+
one can use a mnemonic key:
|
135
|
+
|
136
|
+
| long key | short key | DIGITS | BASE NUMBER |
|
137
|
+
| -------------- | --------- | ------------- | ----------- |
|
138
|
+
| `:graph` | `:g` | `GRAPH` | 94 |
|
139
|
+
| `:qgraph` | `:q` | `QGRAPH` | 91 |
|
140
|
+
| `:base64` | `:b64` | `BASE64` | 64 |
|
141
|
+
| `:word_` | `:w_` | `WORD_` | 63 |
|
142
|
+
| `:word` | `:w` | `WORD` | 62 |
|
143
|
+
| `:unambiguous` | `:u` | `UNAMBIGUOUS` | 47 |
|
144
|
+
|
145
|
+
| long key | short keys | BASE NUMBER |
|
146
|
+
| -------------- | ---------- | ----------- |
|
147
|
+
| `:hexadecimal` | `:hex, :h` | 16 |
|
148
|
+
| `:decimal` | `:dec, :d` | 10 |
|
149
|
+
| `:octal` | `:oct, :o` | 8 |
|
150
|
+
| `:binary` | `:bin, :b` | 2 |
|
153
151
|
|
154
152
|
## LICENSE:
|
155
153
|
|
data/lib/base_convert.rb
CHANGED
@@ -1,9 +1,30 @@
|
|
1
|
+
# http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
|
1
2
|
module BaseConvert
|
2
|
-
VERSION = '
|
3
|
+
VERSION = '3.0.191214'
|
4
|
+
|
5
|
+
def toi(string=@string, base=@base, digits=@digits)
|
6
|
+
integer = 0
|
7
|
+
string.each_char do |c|
|
8
|
+
index = digits.index(c)
|
9
|
+
integer = integer * base + index
|
10
|
+
end
|
11
|
+
integer
|
12
|
+
end
|
13
|
+
|
14
|
+
def tob(integer=@integer, base=@base, digits=@digits)
|
15
|
+
return digits[0] if integer == 0
|
16
|
+
string = ''
|
17
|
+
while integer > 0
|
18
|
+
integer, index = integer.divmod(base)
|
19
|
+
string = string.insert(0, digits[index])
|
20
|
+
end
|
21
|
+
string
|
22
|
+
end
|
23
|
+
|
24
|
+
extend self
|
25
|
+
|
26
|
+
autoload :Configuration, 'base_convert/configuration'
|
27
|
+
autoload :FromTo, 'base_convert/from_to'
|
28
|
+
autoload :Number, 'base_convert/number'
|
3
29
|
end
|
4
|
-
require 'base_convert/configuration'
|
5
|
-
require 'base_convert/functions'
|
6
|
-
require 'base_convert/helpers'
|
7
|
-
require 'base_convert/number'
|
8
|
-
require 'base_convert/from_to'
|
9
30
|
#`ruby`
|
@@ -6,32 +6,54 @@ module Configuration
|
|
6
6
|
|
7
7
|
WORD_ = 0.upto(255).map{|i| i.chr}.select{|c| c=~/\w/}.join.freeze
|
8
8
|
WORD = WORD_.delete('_').freeze
|
9
|
-
INDEXa = WORD.index('a')
|
9
|
+
INDEXa = WORD.index('a')
|
10
10
|
|
11
11
|
AMBIGUOUS = 'B8G6I1l0OQDS5Z2'.freeze
|
12
12
|
UNAMBIGUOUS = WORD.delete(AMBIGUOUS).freeze
|
13
13
|
|
14
|
+
BASE64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
|
15
|
+
|
14
16
|
BASE = {
|
15
17
|
:graph => GRAPH.length,
|
16
18
|
:qgraph => QGRAPH.length,
|
17
19
|
:word_ => WORD_.length,
|
18
20
|
:word => WORD.length,
|
19
21
|
:unambiguous => UNAMBIGUOUS.length,
|
22
|
+
:base64 => 64,
|
23
|
+
:b64 => 64,
|
20
24
|
:hexadecimal => 16,
|
21
25
|
:hex => 16,
|
26
|
+
:h => 16,
|
22
27
|
:decimal => 10,
|
23
28
|
:dec => 10,
|
29
|
+
:d => 10,
|
24
30
|
:octal => 8,
|
25
31
|
:oct => 8,
|
32
|
+
:o => 8,
|
26
33
|
:binary => 2,
|
34
|
+
:bin => 2,
|
35
|
+
:b => 2,
|
27
36
|
}
|
28
37
|
|
38
|
+
BASE[:g] = BASE[:graph]
|
39
|
+
BASE[:q] = BASE[:qgraph]
|
40
|
+
BASE[:w_] = BASE[:word_]
|
41
|
+
BASE[:w] = BASE[:word]
|
42
|
+
BASE[:u] = BASE[:unambiguous]
|
43
|
+
|
29
44
|
DIGITS = {
|
30
45
|
:graph => GRAPH,
|
46
|
+
:g => GRAPH,
|
31
47
|
:qgraph => QGRAPH,
|
32
|
-
:
|
48
|
+
:q => QGRAPH,
|
49
|
+
:word_ => WORD_,
|
50
|
+
:w_ => WORD_,
|
33
51
|
:word => WORD,
|
52
|
+
:w => WORD,
|
34
53
|
:unambiguous => UNAMBIGUOUS,
|
54
|
+
:u => UNAMBIGUOUS,
|
55
|
+
:base64 => BASE64,
|
56
|
+
:b64 => BASE64,
|
35
57
|
}
|
36
58
|
|
37
59
|
end
|
data/lib/base_convert/from_to.rb
CHANGED
@@ -1,35 +1,27 @@
|
|
1
1
|
module BaseConvert
|
2
2
|
class FromTo
|
3
3
|
include Configuration
|
4
|
-
|
5
|
-
extend Helpers
|
4
|
+
include BaseConvert
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
FromTo.validate(@baseto, @to_digits)
|
15
|
-
FromTo.validate(@basefrom, @from_digits)
|
16
|
-
end
|
17
|
-
|
18
|
-
def base2integer(string)
|
19
|
-
string = string.upcase if FromTo.upcase?(@basefrom, @from_digits) # covenience
|
20
|
-
FromTo.validate_string(string, @basefrom, @from_digits)
|
21
|
-
FromTo.to_integer(string, @basefrom, @from_digits)
|
6
|
+
def initialize(base: 10, to_base: base, digits: WORD, to_digits: digits)
|
7
|
+
base = BASE[base] if base.is_a? Symbol
|
8
|
+
to_base = BASE[to_base] if to_base.is_a? Symbol
|
9
|
+
digits = DIGITS[digits] if digits.is_a? Symbol
|
10
|
+
to_digits = DIGITS[to_digits] if to_digits.is_a? Symbol
|
11
|
+
raise "base must cover digits." if base > digits.length or to_base > to_digits.length
|
12
|
+
@base, @to_base, @digits, @to_digits = base, to_base, digits, to_digits
|
22
13
|
end
|
23
|
-
alias base2dec base2integer
|
24
14
|
|
25
|
-
def
|
26
|
-
|
15
|
+
def convert(counter)
|
16
|
+
case counter
|
17
|
+
when Integer
|
18
|
+
tob(counter, @to_base, @to_digits)
|
19
|
+
when String
|
20
|
+
tob(toi(counter), @to_base, @to_digits)
|
21
|
+
else
|
22
|
+
raise "counter must be String|Integer."
|
23
|
+
end
|
27
24
|
end
|
28
|
-
alias
|
29
|
-
|
30
|
-
def convert(string)
|
31
|
-
integer2base base2integer string.to_s
|
32
|
-
end
|
33
|
-
|
25
|
+
alias :[] :convert
|
34
26
|
end
|
35
27
|
end
|
data/lib/base_convert/number.rb
CHANGED
@@ -1,31 +1,144 @@
|
|
1
1
|
module BaseConvert
|
2
|
-
class Number
|
2
|
+
class Number
|
3
3
|
include Configuration
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
@
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
4
|
+
include BaseConvert
|
5
|
+
|
6
|
+
def _infer_digits_from_string
|
7
|
+
if @string.chars.all?{|_|WORD_.include?_}
|
8
|
+
@string.include?('_')? WORD_ : WORD
|
9
|
+
elsif @string.chars.all?{|_|GRAPH.include?_}
|
10
|
+
@string.match?(/['"`]/)? GRAPH : QGRAPH
|
11
|
+
else
|
12
|
+
raise "Need digits."
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def _infer_base_from_string
|
17
|
+
min = 1 + @digits.index(@string.chars.max)
|
18
|
+
max = @digits.length
|
19
|
+
return max if max==min
|
20
|
+
case @digits
|
21
|
+
when WORD, WORD_
|
22
|
+
if min <= 32
|
23
|
+
raise "Need base for WORD or WORD_." if @string.length < 8
|
24
|
+
return 8 if min <= 8
|
25
|
+
return 16 if min <= 16
|
26
|
+
return 32
|
27
|
+
end
|
28
|
+
when UNAMBIGUOUS
|
29
|
+
if min <= 22
|
30
|
+
raise "Need base for UNAMBIGUOUS." if @string.length < 8
|
31
|
+
return 22
|
32
|
+
end
|
33
|
+
when QGRAPH, GRAPH
|
34
|
+
n = @digits.index 'a'
|
35
|
+
if min <= n
|
36
|
+
raise "Need base for QGRAPH or GRAPH." if @string.length < 8
|
37
|
+
return n
|
38
|
+
end
|
39
|
+
end
|
40
|
+
return max
|
41
|
+
end
|
42
|
+
|
43
|
+
def _digits!
|
44
|
+
if @digits.nil?
|
45
|
+
@digits = @integer.nil? ? _infer_digits_from_string : WORD
|
46
|
+
if @base and @base > @digits.length
|
47
|
+
if @base == 64
|
48
|
+
@digits = BASE64
|
49
|
+
elsif @base <= QGRAPH.length
|
50
|
+
@digits = QGRAPH
|
51
|
+
elsif @base <= GRAPH.length
|
52
|
+
@digits = GRAPH
|
53
|
+
else
|
54
|
+
raise "Need digits that can cover base #{@base}."
|
55
|
+
end
|
56
|
+
end
|
57
|
+
else
|
58
|
+
if @digits.is_a? Symbol
|
59
|
+
digits = DIGITS[@digits]
|
60
|
+
raise "Unrecognized digits #{@digits}." if digits.nil?
|
61
|
+
@digits = digits
|
62
|
+
else
|
63
|
+
raise "digits must be a String of at least length 2." unless @digits.is_a?(String) and @digits.length > 2
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def _base!
|
69
|
+
if @base.nil?
|
70
|
+
_digits!
|
71
|
+
if @integer.nil?
|
72
|
+
@base = _infer_base_from_string
|
73
|
+
else
|
74
|
+
@base = @digits.length
|
75
|
+
end
|
76
|
+
else
|
77
|
+
if @base.is_a? Symbol
|
78
|
+
base = BASE[@base]
|
79
|
+
raise "Unrecognized base #{@base}." if base.nil?
|
80
|
+
@base = base
|
81
|
+
else
|
82
|
+
raise "base must be an Integer greater than 1." unless @base.is_a?(Integer) and @base > 1
|
83
|
+
end
|
84
|
+
_digits!
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def _validate
|
89
|
+
raise "digits must cover base." if @base > @digits.length
|
90
|
+
raise "digits must not have duplicates." if @digits.length > @digits.chars.uniq.length
|
91
|
+
unless @string.nil?
|
92
|
+
raise "digits must cover string." unless @string.chars.all?{|_|@digits.include?_}
|
93
|
+
end
|
94
|
+
unless @integer.nil?
|
95
|
+
raise "integer can't be negative." if @integer < 0
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def _integer!
|
100
|
+
_base!
|
101
|
+
_validate if @validate
|
102
|
+
@string.upcase! if @base <= INDEXa and @digits == WORD
|
103
|
+
@integer = toi
|
104
|
+
end
|
105
|
+
|
106
|
+
def _string!
|
107
|
+
_base!
|
108
|
+
_validate if @validate
|
109
|
+
@string = tob
|
110
|
+
end
|
111
|
+
|
112
|
+
attr_reader :base, :digits
|
113
|
+
def initialize(counter, base: nil, digits: nil, validate: true)
|
114
|
+
@base, @digits, @validate = base, digits, validate
|
115
|
+
@string, @integer = nil, nil
|
116
|
+
case counter
|
117
|
+
when String
|
118
|
+
@string = counter
|
119
|
+
_integer!
|
120
|
+
when Integer
|
121
|
+
@integer = counter
|
122
|
+
_string!
|
123
|
+
else
|
124
|
+
raise "Need counter String|Integer."
|
15
125
|
end
|
16
126
|
end
|
17
127
|
|
18
|
-
def
|
19
|
-
|
128
|
+
def to_s
|
129
|
+
@string
|
20
130
|
end
|
21
131
|
|
22
|
-
def
|
23
|
-
|
24
|
-
Number.validate(base, digits) if validate
|
25
|
-
integer = self.to_integer
|
26
|
-
string = Number.to_base(integer, base, digits)
|
27
|
-
Number.new(string, base, digits, false) # no need to validate
|
132
|
+
def to_i
|
133
|
+
@integer
|
28
134
|
end
|
29
135
|
|
136
|
+
def to_base(base, digits=@digits, validate=@validate)
|
137
|
+
Number.new @integer, base: base, digits: digits, validate: validate
|
138
|
+
end
|
139
|
+
|
140
|
+
def to_digits(digits, base=@base, validate=@validate)
|
141
|
+
Number.new @integer, base: base, digits: digits, validate: validate
|
142
|
+
end
|
30
143
|
end
|
31
144
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: base_convert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.191214
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- carlosjhr64
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-12-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: |
|
14
14
|
BaseConvert - Number base conversion.
|
@@ -26,8 +26,6 @@ files:
|
|
26
26
|
- lib/base_convert.rb
|
27
27
|
- lib/base_convert/configuration.rb
|
28
28
|
- lib/base_convert/from_to.rb
|
29
|
-
- lib/base_convert/functions.rb
|
30
|
-
- lib/base_convert/helpers.rb
|
31
29
|
- lib/base_convert/number.rb
|
32
30
|
homepage: https://github.com/carlosjhr64/base_convert
|
33
31
|
licenses:
|
@@ -48,9 +46,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
46
|
- !ruby/object:Gem::Version
|
49
47
|
version: '0'
|
50
48
|
requirements:
|
51
|
-
- 'ruby: ruby 2.
|
52
|
-
|
53
|
-
rubygems_version: 2.6.13
|
49
|
+
- 'ruby: ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]'
|
50
|
+
rubygems_version: 3.0.3
|
54
51
|
signing_key:
|
55
52
|
specification_version: 4
|
56
53
|
summary: BaseConvert - Number base conversion.
|
@@ -1,25 +0,0 @@
|
|
1
|
-
# http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
|
2
|
-
module BaseConvert
|
3
|
-
module Functions
|
4
|
-
|
5
|
-
def to_integer(string, base, digits)
|
6
|
-
integer = 0
|
7
|
-
string.each_char do |c|
|
8
|
-
index = digits.index(c)
|
9
|
-
integer = integer * base + index
|
10
|
-
end
|
11
|
-
integer
|
12
|
-
end
|
13
|
-
|
14
|
-
def to_base(integer, base, digits)
|
15
|
-
return digits[0] if integer == 0
|
16
|
-
string = ''
|
17
|
-
while integer > 0
|
18
|
-
integer, index = integer.divmod(base)
|
19
|
-
string = string.insert(0, digits[index])
|
20
|
-
end
|
21
|
-
string
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
end
|
data/lib/base_convert/helpers.rb
DELETED
@@ -1,37 +0,0 @@
|
|
1
|
-
# http://rosettacode.org/wiki/Non-decimal_radices/Convert#Ruby
|
2
|
-
module BaseConvert
|
3
|
-
module Helpers
|
4
|
-
include Configuration
|
5
|
-
|
6
|
-
def upcase?(base, digits)
|
7
|
-
base <= INDEXa and digits.equal?(WORD)
|
8
|
-
end
|
9
|
-
|
10
|
-
def validate(base, digits)
|
11
|
-
raise 'base is not an integer' unless base.kind_of?(Integer)
|
12
|
-
raise 'digits not an String' unless digits.kind_of?(String)
|
13
|
-
raise 'base not between 2 and digits.length' unless base.between?(2, digits.length)
|
14
|
-
end
|
15
|
-
|
16
|
-
def validate_string(string, base, digits)
|
17
|
-
string.chars.uniq.each do |c|
|
18
|
-
raise 'String has invalid character' unless (i=digits.index(c)) and (i<base)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def digits(base)
|
23
|
-
if base.class == Symbol
|
24
|
-
if digits = DIGITS[base]
|
25
|
-
return digits
|
26
|
-
end
|
27
|
-
base = BASE[base]
|
28
|
-
end
|
29
|
-
(base > WORD.length)? QGRAPH : WORD
|
30
|
-
end
|
31
|
-
|
32
|
-
def base(base)
|
33
|
-
(base.class == Symbol)? BASE[base]: base
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|