bcd 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a942b128f15131db9f456962d2b923e7d930541d
4
+ data.tar.gz: fc7e67e1118d93d5e41238369876fc4cca22af18
5
+ SHA512:
6
+ metadata.gz: c8b73b125c9d09e6a9def5d8c4c732f7ed6dda3ad14d2bcd3d61531cb680c5c1e192099c3e1358f2aa2344b445d835ba8b30415003b1d4c76bc3a59f95ae0e34
7
+ data.tar.gz: 7504b6bdc9ed4438fbc2bbf19ea3de31dbf1fd531ff102640622127b32ee462d96602858241c062a4b0385d9f56559cf833f8a5dd20e7f65c7347a42b31addf7
@@ -0,0 +1 @@
1
+ *gem
@@ -0,0 +1,8 @@
1
+ ruby-bcd
2
+ ==========
3
+
4
+ A binary-coded decimal conversion library for Ruby
5
+
6
+ Installation
7
+ ------------
8
+ `gem install ruby_bcd`
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc "Run tests"
8
+ task :default => :test
@@ -0,0 +1,40 @@
1
+ # Methods to translate to and from binary coded decimal
2
+ #
3
+ # Author:: Dafydd Crosby <dafydd@dafyddcrosby.com>
4
+ # Copyright:: Copyright 2013, Dafydd Crosby
5
+ # License:: BSD 2-clause
6
+
7
+ module BCD
8
+ # Translates binary coded decimal into an integer
9
+ def self.decode(bcd)
10
+ raise ArgumentError, 'Argument is not numeric' unless bcd.is_a? Numeric
11
+ raise ArgumentError, 'Cannot be a negative integer' if bcd < 0
12
+
13
+ binstring = ''
14
+ while true do
15
+ q, r = bcd.divmod(10)
16
+ nibble = r.to_s(2)
17
+ while nibble.length < 4 do
18
+ nibble.prepend('0')
19
+ end
20
+ binstring.prepend(nibble)
21
+ q == 0 ? break : bcd = q
22
+ end
23
+
24
+ binstring.to_i(2)
25
+ end
26
+
27
+ # Translate an integer into binary coded decimal
28
+ def self.encode(int)
29
+ raise ArgumentError, 'Argument is not numeric' unless int.is_a? Numeric
30
+ raise ArgumentError, 'Cannot be a negative integer' if int < 0
31
+
32
+ bcdstring = ''
33
+ while int > 0 do
34
+ nibble = int % 16
35
+ bcdstring.prepend(nibble.to_s)
36
+ int >>= 4
37
+ end
38
+ bcdstring.to_i
39
+ end
40
+ end
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |gem|
2
+ gem.name = 'bcd'
3
+ gem.version = '0.2'
4
+ gem.summary = %q{Binary Coded Decimal library}
5
+ gem.description = %q{A library for decoding and encoding binary coded decimal}
6
+ gem.author = 'Dafydd Crosby'
7
+ gem.email = 'dafydd@dafyddcrosby.com'
8
+ gem.homepage = 'https://github.com/dafyddcrosby/ruby_bcd'
9
+ gem.required_ruby_version = '>= 1.9'
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
+ gem.require_paths = ["lib"]
13
+ gem.license = 'bsd' # (two-clause) BSD license
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'test/unit'
2
+ require 'bcd'
3
+
4
+ class TestBCD < Test::Unit::TestCase
5
+ def test_encode
6
+ assert_equal 4, BCD.encode(4)
7
+ assert_equal 22, BCD.encode(34)
8
+ end
9
+
10
+ def test_decode
11
+ assert_equal 4, BCD.decode(4)
12
+ assert_equal 345, BCD.decode(159)
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bcd
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.2'
5
+ platform: ruby
6
+ authors:
7
+ - Dafydd Crosby
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A library for decoding and encoding binary coded decimal
14
+ email: dafydd@dafyddcrosby.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - .gitignore
20
+ - README.md
21
+ - Rakefile
22
+ - lib/bcd.rb
23
+ - ruby_bcd.gemspec
24
+ - test/test_bcd.rb
25
+ homepage: https://github.com/dafyddcrosby/ruby_bcd
26
+ licenses:
27
+ - bsd
28
+ metadata: {}
29
+ post_install_message:
30
+ rdoc_options: []
31
+ require_paths:
32
+ - lib
33
+ required_ruby_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '1.9'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ requirements: []
44
+ rubyforge_project:
45
+ rubygems_version: 2.0.3
46
+ signing_key:
47
+ specification_version: 4
48
+ summary: Binary Coded Decimal library
49
+ test_files:
50
+ - test/test_bcd.rb