Method: BCD.encode

Defined in:
lib/bcd.rb

.encode(int) ⇒ Object

Translate an integer into binary coded decimal

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bcd.rb', line 28

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