bin2dec 0.0.1

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/bin/bin2dec +9 -0
  3. data/lib/bin2dec.rb +38 -0
  4. metadata +47 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: aa3ae9738ca242a577b66591c11fc588b313dd4e
4
+ data.tar.gz: 54fa78c5d64c7e781bb8ee831f668d1ca79381b8
5
+ SHA512:
6
+ metadata.gz: 4bc743d84eba7ed25f4b9d8092f9ac503d25ca593a33da71482f01faae671a94c03354d96fed8c43a57d86e23f22ac453990ed98f0c15bbcd210adb93b99d456
7
+ data.tar.gz: fce34b1ca0ada2ac8c8b230497aefcfff6ea0e1b2810aae610a25fdc7fd893a2b4f7a27d4a75f5af143d5bec1a4ad5cb34432e6e61badf8bb063cc2aab4ca493
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bin2dec'
4
+
5
+ begin
6
+ puts Bin2dec.convert(ARGV[0])
7
+ rescue Exception => e
8
+ puts e.message
9
+ end
@@ -0,0 +1,38 @@
1
+ class Bin2dec
2
+
3
+ #Algorithm to Convert From Any Base to Base 10 Decimal
4
+
5
+ #Let n be the number of digits in the number. For example, 104 has 3 digits, so n=3.
6
+ #Let b be the base of the number. For example, 104 is decimal so b = 10.
7
+ #Let s be a running total, initially 0.
8
+ #For each digit in the number, working left to right do:
9
+ # Subtract 1 from n.
10
+ # Multiply the digit times b^n and add it to s.
11
+ #When your done with all the digits in the number, its decimal value will be s
12
+
13
+ def self.convert(num)
14
+ n = num.length
15
+ s = 0
16
+
17
+ n.times do | i |
18
+ temp = num[i].to_i
19
+
20
+ if !numeric?(num[i])
21
+ raise "Error: input contains illegal characters"
22
+ end
23
+
24
+ if ((temp == 0) || (temp == 1))
25
+ n-= 1
26
+ s+= temp * (2**n)
27
+ else
28
+ raise "Error: not a binary number"
29
+ end
30
+ end
31
+
32
+ return s
33
+ end
34
+
35
+ def self.numeric?(input)
36
+ input =~ /[0-9]/
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bin2dec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Giancarlo Matute
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - gmatutecr@gmail.com
16
+ executables:
17
+ - bin2dec
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - bin/bin2dec
22
+ - lib/bin2dec.rb
23
+ homepage: http://tute.io
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project: bin2dec
43
+ rubygems_version: 2.6.14
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A tool to convert binary to decimal numbers
47
+ test_files: []