radix 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/HISTORY +17 -2
- data/{README → README.rdoc} +10 -7
- data/lib/radix.rb +1 -106
- data/lib/radix/base.rb +131 -0
- data/lib/radix/meta/data.rb +29 -0
- data/lib/radix/meta/package +8 -0
- data/lib/radix/meta/profile +21 -0
- data/lib/radix/number.rb +84 -0
- data/lib/radix/operator.rb +30 -0
- data/meta/data.rb +29 -0
- data/meta/package +8 -1
- data/meta/profile +21 -0
- data/qed/applique/ae.rb +1 -0
- data/qed/radix_base.rdoc +71 -0
- data/qed/radix_operator.rdoc +27 -0
- metadata +69 -36
- data/MANIFEST +0 -22
- data/demo/examples.rd +0 -73
- data/meta/authors +0 -1
- data/meta/contact +0 -1
- data/meta/created +0 -1
- data/meta/description +0 -1
- data/meta/homepage +0 -1
- data/meta/license +0 -1
- data/meta/project +0 -1
- data/meta/release +0 -1
- data/meta/repository +0 -1
- data/meta/summary +0 -1
- data/meta/version +0 -1
data/HISTORY
CHANGED
@@ -1,11 +1,26 @@
|
|
1
1
|
= Release History
|
2
2
|
|
3
|
-
== 1.
|
3
|
+
== 1.1.0 / 2010-09-03
|
4
|
+
|
5
|
+
Radix now provides an actual Numeric subclass, Radix::Number, that stores
|
6
|
+
the base and can be used like any other Numeric object. This makes it very
|
7
|
+
easy to convert and manipulate numbers in any base. The implementation is still
|
8
|
+
a bit nascent. For the moment, it only supports the most basic math operators
|
9
|
+
and only handles integer values, but furture releases will continue to expand
|
10
|
+
on it's capabilites.
|
11
|
+
|
12
|
+
Changes:
|
13
|
+
|
14
|
+
* Add Radix::Number to handle bases like any other numeric.
|
15
|
+
* Rename Radix class to Radix::Base.
|
16
|
+
|
17
|
+
|
18
|
+
== 1.0.0 / 2009-07-01
|
4
19
|
|
5
20
|
This is the initial stand-alone release of Radix,
|
6
21
|
ported from the basex.rb library of Ruby Facets.
|
7
22
|
|
8
23
|
Changes:
|
9
24
|
|
10
|
-
|
25
|
+
* Happy Birthday!
|
11
26
|
|
data/{README → README.rdoc}
RENAMED
@@ -1,14 +1,15 @@
|
|
1
1
|
= Radix
|
2
2
|
|
3
|
-
* http://
|
4
|
-
* http://
|
3
|
+
* http://rubyworks.github.com/radix
|
4
|
+
* http://github.com/rubyworks/radix
|
5
5
|
|
6
6
|
|
7
7
|
== DESCRIPTION
|
8
8
|
|
9
9
|
Radix provides the means of converting to and from any base.
|
10
|
+
|
10
11
|
In addition, representational notations need not be in
|
11
|
-
ASCII order
|
12
|
+
ASCII order --any user-defined notation can be used up to
|
12
13
|
base 62.
|
13
14
|
|
14
15
|
|
@@ -36,16 +37,18 @@ But Ruby reaches it's limit at base 36.
|
|
36
37
|
|
37
38
|
255.to_s(37) #=> Error
|
38
39
|
|
39
|
-
Radix provides the means of converting to and from any base.
|
40
|
+
Radix provides the means of converting to and from any base. For example,
|
41
|
+
a number in base 256, represented by the array [100, 10] (ie. 100 * 256 + 10 * 1),
|
42
|
+
can be converted to base 10 as follows:
|
40
43
|
|
41
44
|
Radix.convert_base([100, 10], 256, 10)
|
42
45
|
#=> [2,5,6,1,0]
|
43
46
|
|
44
|
-
And it can handle any notation
|
47
|
+
And it can handle any string notation up to base 62.
|
45
48
|
|
46
49
|
Radix.convert("10", 62, 10) #=> "62"
|
47
50
|
|
48
|
-
|
51
|
+
The string notation need not be in ASCII order --odd notations
|
49
52
|
can be used.
|
50
53
|
|
51
54
|
b10 = Radix.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
|
@@ -70,7 +73,7 @@ Windows users use 'ruby setup.rb all'.
|
|
70
73
|
|
71
74
|
== LINCENSE/COPYRIGHT
|
72
75
|
|
73
|
-
Copyright (c)
|
76
|
+
Copyright (c) 2009 Thomas Sawyer
|
74
77
|
|
75
78
|
This program is ditributed unser the terms of the LGPLv3 license.
|
76
79
|
|
data/lib/radix.rb
CHANGED
@@ -1,106 +1 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# Base conversions with ASCII ordered notations are easy in Ruby.
|
4
|
-
#
|
5
|
-
# 255.to_s(16) #=> "FF"
|
6
|
-
# "FF".to_i(16) #=> 255
|
7
|
-
#
|
8
|
-
# But Ruby reaches it's limit at base 36.
|
9
|
-
#
|
10
|
-
# 255.to_s(37) #=> Error
|
11
|
-
#
|
12
|
-
# Radix provides the means of converting to and from any base.
|
13
|
-
#
|
14
|
-
# Radix.convert_base([100, 10], 256, 10)
|
15
|
-
# #=> [2,5,6,1,0]
|
16
|
-
#
|
17
|
-
# And it can handle any notation upto base 62.
|
18
|
-
#
|
19
|
-
# Radix.convert("10", 62, 10) #=> "62"
|
20
|
-
#
|
21
|
-
# And the notations need not be in ASCII order --odd notations
|
22
|
-
# can be used.
|
23
|
-
#
|
24
|
-
# b10 = Radix.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
|
25
|
-
# b10.convert("FF", 16) #=> "EYY"
|
26
|
-
#
|
27
|
-
class Radix
|
28
|
-
|
29
|
-
BASE10 = ["0".."9"].map { |r| r.to_a }.flatten
|
30
|
-
BASE12 = ["0".."9", ["X", "E"]].map { |r| r.to_a }.flatten
|
31
|
-
BASE16 = ["0".."9", "A".."F"].map { |r| r.to_a }.flatten
|
32
|
-
BASE36 = ["0".."9", "A".."Z"].map { |r| r.to_a }.flatten
|
33
|
-
BASE60 = ["0".."9", "a".."z", "A".."X"].map { |r| r.to_a }.flatten
|
34
|
-
BASE62 = ["0".."9", "a".."z", "A".."Z"].map { |r| r.to_a }.flatten
|
35
|
-
|
36
|
-
attr :chars
|
37
|
-
attr :base
|
38
|
-
attr :values
|
39
|
-
|
40
|
-
def initialize(chars=BASE62)
|
41
|
-
@chars = chars.map{ |c| c.to_s }
|
42
|
-
@base = @chars.size
|
43
|
-
@values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten]
|
44
|
-
end
|
45
|
-
|
46
|
-
#
|
47
|
-
def encode(byte_string)
|
48
|
-
digits = byte_string.unpack("C*")
|
49
|
-
digits = convert_base(digits, 256, base)
|
50
|
-
digits.map{ |d| @chars[d] }.join
|
51
|
-
end
|
52
|
-
|
53
|
-
#
|
54
|
-
def decode(encoded)
|
55
|
-
digits = encoded.split(//).map{ |c| @values[c] }
|
56
|
-
convert_base(digits, base, 256).pack("C*")
|
57
|
-
end
|
58
|
-
|
59
|
-
#
|
60
|
-
def convert(digits, from_radix)
|
61
|
-
from_radix = standard_radix(from_radix) if Integer === from_radix
|
62
|
-
digits = digits.to_s.split(//)
|
63
|
-
digits = digits.map{ |digit| from_radix.values[digit] }
|
64
|
-
digits = convert_base(digits, from_radix.base, base)
|
65
|
-
digits = digits.map{ |digit| chars[digit] }
|
66
|
-
digits.join
|
67
|
-
end
|
68
|
-
|
69
|
-
#
|
70
|
-
def convert_base(digits, from_base, to_base)
|
71
|
-
self.class.convert_base(digits, from_base, to_base)
|
72
|
-
end
|
73
|
-
|
74
|
-
def standard_radix(integer_base)
|
75
|
-
self.class.standard_radix(integer_base)
|
76
|
-
end
|
77
|
-
|
78
|
-
# Do a standard conversion upto base 62.
|
79
|
-
def self.convert(digits, from_base, to_base)
|
80
|
-
r1 = standard_radix(from_base)
|
81
|
-
r2 = standard_radix(to_base)
|
82
|
-
r2.convert(digits, r1)
|
83
|
-
end
|
84
|
-
|
85
|
-
#
|
86
|
-
def self.convert_base(digits, from_base, to_base)
|
87
|
-
bignum = 0
|
88
|
-
digits.each { |digit| bignum = bignum * from_base + digit }
|
89
|
-
converted = []
|
90
|
-
until bignum.zero?
|
91
|
-
bignum, digit = bignum.divmod(to_base)
|
92
|
-
converted.push(digit)
|
93
|
-
end
|
94
|
-
converted.reverse
|
95
|
-
end
|
96
|
-
|
97
|
-
#
|
98
|
-
def self.standard_radix(integer_base)
|
99
|
-
if integer_base > 36
|
100
|
-
Radix.new(BASE62[0..integer_base-1])
|
101
|
-
else
|
102
|
-
Radix.new(BASE36[0..integer_base-1])
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
end
|
1
|
+
require 'radix/operator'
|
data/lib/radix/base.rb
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
# Radix coverts to and from any base.
|
2
|
+
#
|
3
|
+
# Base conversions with ASCII ordered notations are easy in Ruby.
|
4
|
+
#
|
5
|
+
# 255.to_s(16) #=> "FF"
|
6
|
+
# "FF".to_i(16) #=> 255
|
7
|
+
#
|
8
|
+
# But Ruby reaches it's limit at base 36.
|
9
|
+
#
|
10
|
+
# 255.to_s(37) #=> Error
|
11
|
+
#
|
12
|
+
# Radix provides the means of converting to and from any base.
|
13
|
+
#
|
14
|
+
# Radix::Base.convert_base([100, 10], 256, 10)
|
15
|
+
# #=> [2,5,6,1,0]
|
16
|
+
#
|
17
|
+
# And it can handle any notation upto base 62.
|
18
|
+
#
|
19
|
+
# Radix::Base.convert("10", 62, 10) #=> "62"
|
20
|
+
#
|
21
|
+
# And the notations need not be in ASCII order --odd notations
|
22
|
+
# can be used.
|
23
|
+
#
|
24
|
+
# b10 = Radix::Base.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
|
25
|
+
# b10.convert("FF", 16) #=> "EYY"
|
26
|
+
#
|
27
|
+
module Radix
|
28
|
+
|
29
|
+
class Base
|
30
|
+
|
31
|
+
BASE10 = ["0".."9"].map { |r| r.to_a }.flatten
|
32
|
+
BASE12 = ["0".."9", ["X", "E"]].map { |r| r.to_a }.flatten
|
33
|
+
BASE16 = ["0".."9", "A".."F"].map { |r| r.to_a }.flatten
|
34
|
+
BASE36 = ["0".."9", "A".."Z"].map { |r| r.to_a }.flatten
|
35
|
+
BASE60 = ["0".."9", "a".."z", "A".."X"].map { |r| r.to_a }.flatten
|
36
|
+
BASE62 = ["0".."9", "a".."z", "A".."Z"].map { |r| r.to_a }.flatten
|
37
|
+
|
38
|
+
attr :chars
|
39
|
+
attr :base
|
40
|
+
attr :values
|
41
|
+
|
42
|
+
# New Radix using +chars+ representation.
|
43
|
+
def initialize(chars=BASE62)
|
44
|
+
@chars = chars.map{ |c| c.to_s }
|
45
|
+
@base = @chars.size
|
46
|
+
@values = Hash[*(0...@base).map { |i| [ @chars[i], i ] }.flatten]
|
47
|
+
end
|
48
|
+
|
49
|
+
# Encode a string in the radix.
|
50
|
+
def encode(byte_string)
|
51
|
+
digits = byte_string.unpack("C*")
|
52
|
+
digits = convert_base(digits, 256, base)
|
53
|
+
digits.map{ |d| @chars[d] }.join
|
54
|
+
end
|
55
|
+
|
56
|
+
# Decode a string that was previously encoded in the radix.
|
57
|
+
def decode(encoded)
|
58
|
+
digits = encoded.split(//).map{ |c| @values[c] }
|
59
|
+
convert_base(digits, base, 256).pack("C*")
|
60
|
+
end
|
61
|
+
|
62
|
+
# Convert a representational +number+ of +from_radix+ to the radix.
|
63
|
+
def convert(number, from_radix)
|
64
|
+
from_radix = standard_radix(from_radix) if Integer === from_radix
|
65
|
+
digits = number.to_s.split(//)
|
66
|
+
digits = digits.map{ |digit| from_radix.values[digit] }
|
67
|
+
digits = convert_base(digits, from_radix.base, base)
|
68
|
+
digits = digits.map{ |digit| chars[digit] }
|
69
|
+
digits.join
|
70
|
+
end
|
71
|
+
|
72
|
+
# Convert any base to any other base, using array of +digits+.
|
73
|
+
def convert_base(digits, from_base, to_base)
|
74
|
+
self.class.convert_base(digits, from_base, to_base)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def standard_radix(integer_base)
|
80
|
+
self.class.standard_radix(integer_base)
|
81
|
+
end
|
82
|
+
|
83
|
+
public
|
84
|
+
|
85
|
+
# Do a standard conversion upto base 62.
|
86
|
+
def self.convert(number, from_base, to_base)
|
87
|
+
r1 = standard_radix(from_base)
|
88
|
+
r2 = standard_radix(to_base)
|
89
|
+
r2.convert(number, r1)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Convert any base to any other base, using array of +digits+.
|
93
|
+
def self.convert_base(digits, from_base, to_base)
|
94
|
+
bignum = 0
|
95
|
+
digits.each { |digit| bignum = bignum * from_base + digit }
|
96
|
+
converted = []
|
97
|
+
until bignum.zero?
|
98
|
+
bignum, digit = bignum.divmod(to_base)
|
99
|
+
converted.push(digit)
|
100
|
+
end
|
101
|
+
converted.reverse
|
102
|
+
end
|
103
|
+
|
104
|
+
# Provide a standard representation of a base upto 62.
|
105
|
+
def self.standard_radix(integer_base)
|
106
|
+
if integer_base > 36
|
107
|
+
new(BASE62[0..integer_base-1])
|
108
|
+
else
|
109
|
+
new(BASE36[0..integer_base-1])
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
|
115
|
+
# Do a standard conversion upto base 62.
|
116
|
+
def self.convert(number, from_base, to_base)
|
117
|
+
Radix::Base.convert(number, from_base, to_base)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Convert any base to any other base, using array of +digits+.
|
121
|
+
def self.convert_base(digits, from_base, to_base)
|
122
|
+
Radix::Base.convert_base(digits, from_base, to_base)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Provide a standard representation of a base upto 62.
|
126
|
+
def self.standard_radix(integer_base)
|
127
|
+
Radix::Base.standard_radix(integer_base)
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
Object.__send__(:remove_const, :VERSION) if Object.const_defined?(:VERSION) # becuase Ruby 1.8~ gets in the way
|
2
|
+
|
3
|
+
module Radix
|
4
|
+
|
5
|
+
def self.__DIR__
|
6
|
+
File.dirname(__FILE__)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.package
|
10
|
+
@package ||= (
|
11
|
+
require 'yaml'
|
12
|
+
YAML.load(File.new(__DIR__ + '/package'))
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.profile
|
17
|
+
@profile ||= (
|
18
|
+
require 'yaml'
|
19
|
+
YAML.load(File.new(__DIR__ + '/profile'))
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.const_missing(name)
|
24
|
+
key = name.to_s.downcase
|
25
|
+
package[key] || profile[key] || super(name)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
title : Radix
|
3
|
+
suite : rubyworks
|
4
|
+
summary: Convert to and from any base.
|
5
|
+
contact: trans <transfire@gmail.com>
|
6
|
+
license: LGPL
|
7
|
+
authors: Thomas Sawyer
|
8
|
+
created: 2009-07-01
|
9
|
+
|
10
|
+
description:
|
11
|
+
Convert to and from any base.
|
12
|
+
|
13
|
+
resources:
|
14
|
+
hompage : http://rubyworks.github.com/radix
|
15
|
+
development: http://github.com/rubyworks/radix
|
16
|
+
mailinglist: http://groups.google.com/group/rubyworks-mailinglist
|
17
|
+
repository : git://github.com/rubyworks/radix.git
|
18
|
+
|
19
|
+
copyright:
|
20
|
+
Copyright (c) 2009 Thomas Sawyer
|
21
|
+
|
data/lib/radix/number.rb
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'radix/base'
|
2
|
+
|
3
|
+
module Radix
|
4
|
+
|
5
|
+
#
|
6
|
+
class Number < Numeric
|
7
|
+
|
8
|
+
attr :digits
|
9
|
+
|
10
|
+
attr :base
|
11
|
+
|
12
|
+
def initialize(value, base)
|
13
|
+
case value
|
14
|
+
when ::Array
|
15
|
+
@digits = value
|
16
|
+
when ::String
|
17
|
+
@digits = value.to_s.split(//).map{ |e| Radix.convert(e, base, 10).to_i }
|
18
|
+
when ::Numeric
|
19
|
+
@digits = value.to_s.split(//).map{ |e| e.to_i }
|
20
|
+
end
|
21
|
+
@base = base
|
22
|
+
end
|
23
|
+
|
24
|
+
def +(other)
|
25
|
+
operation(:+, other)
|
26
|
+
end
|
27
|
+
|
28
|
+
def -(other)
|
29
|
+
operation(:-, other)
|
30
|
+
end
|
31
|
+
|
32
|
+
def *(other)
|
33
|
+
operation(:*, other)
|
34
|
+
end
|
35
|
+
|
36
|
+
def /(other)
|
37
|
+
operation(:/, other)
|
38
|
+
end
|
39
|
+
|
40
|
+
def ==(other)
|
41
|
+
case other
|
42
|
+
when Radix::Number
|
43
|
+
if base == other.base
|
44
|
+
digits == other.digits
|
45
|
+
else
|
46
|
+
digits == other.convert(base).digits
|
47
|
+
end
|
48
|
+
else
|
49
|
+
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
#
|
54
|
+
def convert(new_base)
|
55
|
+
new_digits = Radix::Base.convert_base(digits, base, new_base)
|
56
|
+
self.class.new(new_digits, new_base)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def operation(op, other)
|
62
|
+
case other
|
63
|
+
when Radix::Number
|
64
|
+
s = Radix::Base.convert_base(self.digits , self.base , 10)
|
65
|
+
o = Radix::Base.convert_base(other.digits, other.base, 10)
|
66
|
+
|
67
|
+
s = s.join.to_i
|
68
|
+
o = o.join.to_i
|
69
|
+
|
70
|
+
r = s.__send__(op, o)
|
71
|
+
|
72
|
+
r = r.to_s.split(//).map{ |e| e.to_i }
|
73
|
+
|
74
|
+
n = Radix::Base.convert_base(r, 10, self.base)
|
75
|
+
|
76
|
+
Radix::Number.new(n, self.base)
|
77
|
+
else
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'radix/number'
|
2
|
+
|
3
|
+
class ::Numeric
|
4
|
+
|
5
|
+
#
|
6
|
+
def b(base)
|
7
|
+
Radix::Number.new(self, base)
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
|
13
|
+
class ::String
|
14
|
+
|
15
|
+
#
|
16
|
+
def b(base)
|
17
|
+
Radix::Number.new(self, base)
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
class ::Array
|
24
|
+
|
25
|
+
#
|
26
|
+
def b(base)
|
27
|
+
Radix::Number.new(self, base)
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
data/meta/data.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
Object.__send__(:remove_const, :VERSION) if Object.const_defined?(:VERSION) # becuase Ruby 1.8~ gets in the way
|
2
|
+
|
3
|
+
module Radix
|
4
|
+
|
5
|
+
def self.__DIR__
|
6
|
+
File.dirname(__FILE__)
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.package
|
10
|
+
@package ||= (
|
11
|
+
require 'yaml'
|
12
|
+
YAML.load(File.new(__DIR__ + '/package'))
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.profile
|
17
|
+
@profile ||= (
|
18
|
+
require 'yaml'
|
19
|
+
YAML.load(File.new(__DIR__ + '/profile'))
|
20
|
+
)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.const_missing(name)
|
24
|
+
key = name.to_s.downcase
|
25
|
+
package[key] || profile[key] || super(name)
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
29
|
+
|
data/meta/package
CHANGED
data/meta/profile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
---
|
2
|
+
title : Radix
|
3
|
+
suite : rubyworks
|
4
|
+
summary: Convert to and from any base.
|
5
|
+
contact: trans <transfire@gmail.com>
|
6
|
+
license: LGPL
|
7
|
+
authors: Thomas Sawyer
|
8
|
+
created: 2009-07-01
|
9
|
+
|
10
|
+
description:
|
11
|
+
Convert to and from any base.
|
12
|
+
|
13
|
+
resources:
|
14
|
+
hompage : http://rubyworks.github.com/radix
|
15
|
+
development: http://github.com/rubyworks/radix
|
16
|
+
mailinglist: http://groups.google.com/group/rubyworks-mailinglist
|
17
|
+
repository : git://github.com/rubyworks/radix.git
|
18
|
+
|
19
|
+
copyright:
|
20
|
+
Copyright (c) 2009 Thomas Sawyer
|
21
|
+
|
data/qed/applique/ae.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ae/should'
|
data/qed/radix_base.rdoc
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
= Overview of Radix::::Base
|
2
|
+
|
3
|
+
First require the library.
|
4
|
+
|
5
|
+
require 'radix/base'
|
6
|
+
|
7
|
+
First let's try something we all know, hexideciaml.
|
8
|
+
First we setup the radix for each.
|
9
|
+
|
10
|
+
b10 = Radix::Base.new(Radix::Base::BASE10)
|
11
|
+
b16 = Radix::Base.new(Radix::Base::BASE16)
|
12
|
+
|
13
|
+
Now we can covert from one base to the other.
|
14
|
+
|
15
|
+
b16.convert("16" , b10).should == "10"
|
16
|
+
b16.convert("160", b10).should == "A0"
|
17
|
+
b16.convert("255", b10).should == "FF"
|
18
|
+
|
19
|
+
To confirm, lets convert from deciaml to hexidecimal.
|
20
|
+
|
21
|
+
b10.convert("10", b16).should == "16"
|
22
|
+
b10.convert("A0", b16).should == "160"
|
23
|
+
b10.convert("FF", b16).should == "255"
|
24
|
+
|
25
|
+
If we we want to use standard encodings then we
|
26
|
+
can simply provide an integer base, rather than
|
27
|
+
a Radix object.
|
28
|
+
|
29
|
+
b10.convert("10", 16).should == "16"
|
30
|
+
b10.convert("A0", 16).should == "160"
|
31
|
+
b10.convert("FF", 16).should == "255"
|
32
|
+
|
33
|
+
We can also use the module function to convert to and from standard
|
34
|
+
notations upto 62 without creating an instance of Radix::Base.
|
35
|
+
|
36
|
+
Radix::Base.convert("10", 16, 10).should == "16"
|
37
|
+
Radix::Base.convert("A0", 16, 10).should == "160"
|
38
|
+
Radix::Base.convert("FF", 16, 10).should == "255"
|
39
|
+
|
40
|
+
Let's try that again with the maximum base supported.
|
41
|
+
|
42
|
+
Radix::Base.convert( "62", 10, 62).should == "10"
|
43
|
+
Radix::Base.convert("8814542", 10, 62).should == "AZ42"
|
44
|
+
|
45
|
+
Radix::Base.convert( "10", 62, 10).should == "62"
|
46
|
+
Radix::Base.convert( "AZ42", 62, 10).should == "8814542"
|
47
|
+
|
48
|
+
Radix can also be used to encode and decode strings.
|
49
|
+
|
50
|
+
b16.encode("CHARLIE").should == "434841524C4945"
|
51
|
+
b16.decode("434841524C4945").should == "CHARLIE"
|
52
|
+
|
53
|
+
Now let's try a more down to earth base, my favorite,
|
54
|
+
senary, or base six.
|
55
|
+
|
56
|
+
b6 = Radix::Base.new(0..5)
|
57
|
+
b6.convert("39", 10).should == "103"
|
58
|
+
|
59
|
+
And the notations need not be in ASCII order. Odd alternate notations
|
60
|
+
can be used as well.
|
61
|
+
|
62
|
+
b10 = Radix::Base.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
|
63
|
+
b10.convert("FF", 16) #=> "EYY"
|
64
|
+
|
65
|
+
Finally, we will demonstrate how to convert bases larger than 62.
|
66
|
+
These can only be represented as arrays since there are not enough
|
67
|
+
latin characters to represent them.
|
68
|
+
|
69
|
+
Radix::Base.convert_base([100, 10], 256, 10).should == [2, 5, 6, 1, 0]
|
70
|
+
Radix::Base.convert_base([2, 5, 6, 1, 0], 10, 256).should == [100, 10]
|
71
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
= Radix Base Operator
|
2
|
+
|
3
|
+
There first version of Radix worked well enough,
|
4
|
+
but its API was very functional and thus a bit difficult
|
5
|
+
to read at a glance.
|
6
|
+
|
7
|
+
To improve upon this Radix now offers an option operater
|
8
|
+
API.
|
9
|
+
|
10
|
+
require 'radix'
|
11
|
+
|
12
|
+
With it, the #b method extends String and Array classes to
|
13
|
+
simplify all mulit-base operations.
|
14
|
+
|
15
|
+
r = "1000".b(2) + "2".b(8)
|
16
|
+
r.assert == "1010".b(2)
|
17
|
+
r.assert == "12".b(8)
|
18
|
+
r.assert == "10".b(10)
|
19
|
+
|
20
|
+
r = "AZ42".b(62) + "54".b(10)
|
21
|
+
r.assert == "8814596".b(10)
|
22
|
+
|
23
|
+
Working with arrays for bases greater than 62.
|
24
|
+
|
25
|
+
r = [100,10].b(256) + "54".b(10)
|
26
|
+
r.assert == "25664".b(10)
|
27
|
+
|
metadata
CHANGED
@@ -1,77 +1,110 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: radix
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 19
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
|
-
-
|
13
|
+
- Thomas Sawyer
|
8
14
|
autorequire:
|
9
15
|
bindir: bin
|
10
16
|
cert_chain: []
|
11
17
|
|
12
|
-
date:
|
18
|
+
date: 2010-09-03 00:00:00 -04:00
|
13
19
|
default_executable:
|
14
|
-
dependencies:
|
15
|
-
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: syckle
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: qed
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id002
|
16
49
|
description: Convert to and from any base.
|
17
|
-
email:
|
50
|
+
email: transfire@gmail.com
|
18
51
|
executables: []
|
19
52
|
|
20
53
|
extensions: []
|
21
54
|
|
22
55
|
extra_rdoc_files:
|
23
|
-
- README
|
24
|
-
- MANIFEST
|
25
|
-
- LICENSE
|
26
|
-
- HISTORY
|
56
|
+
- README.rdoc
|
27
57
|
files:
|
28
|
-
-
|
29
|
-
-
|
30
|
-
-
|
31
|
-
-
|
32
|
-
-
|
58
|
+
- lib/radix/base.rb
|
59
|
+
- lib/radix/meta/data.rb
|
60
|
+
- lib/radix/meta/package
|
61
|
+
- lib/radix/meta/profile
|
62
|
+
- lib/radix/number.rb
|
63
|
+
- lib/radix/operator.rb
|
33
64
|
- lib/radix.rb
|
34
|
-
- meta/
|
35
|
-
- meta/contact
|
36
|
-
- meta/created
|
37
|
-
- meta/description
|
38
|
-
- meta/homepage
|
39
|
-
- meta/license
|
65
|
+
- meta/data.rb
|
40
66
|
- meta/package
|
41
|
-
- meta/
|
42
|
-
-
|
43
|
-
-
|
44
|
-
-
|
45
|
-
-
|
67
|
+
- meta/profile
|
68
|
+
- qed/applique/ae.rb
|
69
|
+
- qed/radix_base.rdoc
|
70
|
+
- qed/radix_operator.rdoc
|
71
|
+
- LICENSE
|
72
|
+
- README.rdoc
|
73
|
+
- HISTORY
|
46
74
|
has_rdoc: true
|
47
|
-
homepage:
|
48
|
-
licenses:
|
49
|
-
|
75
|
+
homepage: ""
|
76
|
+
licenses:
|
77
|
+
- LGPL
|
50
78
|
post_install_message:
|
51
79
|
rdoc_options:
|
52
|
-
- --inline-source
|
53
80
|
- --title
|
54
|
-
-
|
81
|
+
- Radix API
|
55
82
|
- --main
|
56
|
-
- README
|
83
|
+
- README.rdoc
|
57
84
|
require_paths:
|
58
85
|
- lib
|
59
86
|
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
60
88
|
requirements:
|
61
89
|
- - ">="
|
62
90
|
- !ruby/object:Gem::Version
|
91
|
+
hash: 3
|
92
|
+
segments:
|
93
|
+
- 0
|
63
94
|
version: "0"
|
64
|
-
version:
|
65
95
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
66
97
|
requirements:
|
67
98
|
- - ">="
|
68
99
|
- !ruby/object:Gem::Version
|
100
|
+
hash: 3
|
101
|
+
segments:
|
102
|
+
- 0
|
69
103
|
version: "0"
|
70
|
-
version:
|
71
104
|
requirements: []
|
72
105
|
|
73
|
-
rubyforge_project:
|
74
|
-
rubygems_version: 1.3.
|
106
|
+
rubyforge_project: radix
|
107
|
+
rubygems_version: 1.3.7
|
75
108
|
signing_key:
|
76
109
|
specification_version: 3
|
77
110
|
summary: Convert to and from any base.
|
data/MANIFEST
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
HISTORY
|
2
|
-
LICENSE
|
3
|
-
MANIFEST
|
4
|
-
README
|
5
|
-
demo
|
6
|
-
demo/examples.rd
|
7
|
-
lib
|
8
|
-
lib/radix.rb
|
9
|
-
meta
|
10
|
-
meta/authors
|
11
|
-
meta/contact
|
12
|
-
meta/created
|
13
|
-
meta/description
|
14
|
-
meta/homepage
|
15
|
-
meta/license
|
16
|
-
meta/package
|
17
|
-
meta/project
|
18
|
-
meta/release
|
19
|
-
meta/repository
|
20
|
-
meta/summary
|
21
|
-
meta/version
|
22
|
-
test
|
data/demo/examples.rd
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
= Examples of Radix
|
2
|
-
|
3
|
-
First require the library.
|
4
|
-
|
5
|
-
require 'radix'
|
6
|
-
|
7
|
-
First let's try something we all know, hexideciaml.
|
8
|
-
First we setup the radix for each.
|
9
|
-
|
10
|
-
b10 = Radix.new(Radix::BASE10)
|
11
|
-
b16 = Radix.new(Radix::BASE16)
|
12
|
-
|
13
|
-
Now we can covert from one to the other.
|
14
|
-
|
15
|
-
b16.convert("16" , b10).should == "10"
|
16
|
-
b16.convert("160", b10).should == "A0"
|
17
|
-
b16.convert("255", b10).should == "FF"
|
18
|
-
|
19
|
-
To Confirm, we convert from deciaml to hexidecimal.
|
20
|
-
|
21
|
-
b10.convert("10", b16).should == "16"
|
22
|
-
b10.convert("A0", b16).should == "160"
|
23
|
-
b10.convert("FF", b16).should == "255"
|
24
|
-
|
25
|
-
If we we want to use standard encodings then we
|
26
|
-
can simple provide an integer base, rather than
|
27
|
-
a Radix class.
|
28
|
-
|
29
|
-
b10.convert("10", 16).should == "16"
|
30
|
-
b10.convert("A0", 16).should == "160"
|
31
|
-
b10.convert("FF", 16).should == "255"
|
32
|
-
|
33
|
-
We can also use the module function to convert
|
34
|
-
to and from standard notations upto 62 without
|
35
|
-
creating an instance of Radix.
|
36
|
-
|
37
|
-
Radix.convert("10", 16, 10).should == "16"
|
38
|
-
Radix.convert("A0", 16, 10).should == "160"
|
39
|
-
Radix.convert("FF", 16, 10).should == "255"
|
40
|
-
|
41
|
-
Let's try that again with the mazimum base supported.
|
42
|
-
|
43
|
-
Radix.convert( "62", 10, 62).should == "10"
|
44
|
-
Radix.convert("8814542", 10, 62).should == "AZ42"
|
45
|
-
|
46
|
-
Radix.convert( "10", 62, 10).should == "62"
|
47
|
-
Radix.convert( "AZ42", 62, 10).should == "8814542"
|
48
|
-
|
49
|
-
Radix can also be used to encode and decode strings.
|
50
|
-
|
51
|
-
b16.encode("CHARLIE").should == "434841524C4945"
|
52
|
-
b16.decode("434841524C4945").should == "CHARLIE"
|
53
|
-
|
54
|
-
Now let's try a more down to earth base, my favorite,
|
55
|
-
senary, or base six.
|
56
|
-
|
57
|
-
b6 = Radix.new(0..5)
|
58
|
-
b6.convert("39", 10).should == "103"
|
59
|
-
|
60
|
-
And the notations need not be in ASCII order. Odd alternate notations
|
61
|
-
can be used as well.
|
62
|
-
|
63
|
-
b10 = Radix.new([:Q, :W, :E, :R, :T, :Y, :U, :I, :O, :U])
|
64
|
-
b10.convert("FF", 16) #=> "EYY"
|
65
|
-
|
66
|
-
Finally, we will demonstrate how to convert bases larger than 62.
|
67
|
-
These can only be represented as arrays since there are not enough
|
68
|
-
latin characters to represent them.
|
69
|
-
|
70
|
-
Radix.convert_base([100, 10], 256, 10).should == [2, 5, 6, 1, 0]
|
71
|
-
Radix.convert_base([2, 5, 6, 1, 0], 10, 256).should == [100, 10]
|
72
|
-
|
73
|
-
QED.
|
data/meta/authors
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
trans <transfire@gmail.com>
|
data/meta/contact
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
trans <transfire@gmail.com>
|
data/meta/created
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2006-08-01
|
data/meta/description
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Convert to and from any base.
|
data/meta/homepage
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
http://death.rubyforge.org/radix
|
data/meta/license
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
LGPL
|
data/meta/project
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
death
|
data/meta/release
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
2009-06-26
|
data/meta/repository
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
svn://rubyforge.org/var/svn/death/radix
|
data/meta/summary
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
Convert to and from any base.
|
data/meta/version
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
1.0.0
|