Sha256: e03d777cb04268f3140375d981dc4cb318235be303c0af4a3865b7ca52e3d80c

Contents?: true

Size: 1.02 KB

Versions: 2

Compression:

Stored size: 1.02 KB

Contents

# Methods to translate to and from binary coded decimal
#
# Author:: Dafydd Crosby <dafydd@dafyddcrosby.com>
# Copyright:: Copyright 2013, Dafydd Crosby
# License:: BSD 2-clause

module BCD
  # Translates binary coded decimal into an integer
  def self.decode(bcd)
    raise ArgumentError, 'Argument is not numeric' unless bcd.is_a? Numeric
    raise ArgumentError, 'Cannot be a negative integer' if bcd < 0

    binstring = ''
    while true do
      q, r = bcd.divmod(10)
      nibble = r.to_s(2)
      while nibble.length < 4 do
        nibble.prepend('0')
      end
      binstring.prepend(nibble)
      q == 0 ? break : bcd = q
    end

    binstring.to_i(2)
  end

  # Translate an integer into binary coded decimal
  def self.encode(int)
    raise ArgumentError, 'Argument is not numeric' unless int.is_a? Numeric
    raise ArgumentError, 'Cannot be a negative integer' if int < 0

    bcdstring = ''
    while int > 0 do
      nibble = int % 16
      bcdstring.prepend(nibble.to_s)
      int >>= 4
    end
    bcdstring.to_i
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bcd-0.3 lib/bcd.rb
bcd-0.2 lib/bcd.rb