class ASMOperations::Hex

Attributes

hex[RW]

Public Class Methods

new(hex) click to toggle source
Calls superclass method ASMOperations::Binary::new
# File lib/types/hex.rb, line 5
def initialize(hex)
  @hex = hex
  super(to_binary)
end

Public Instance Methods

fix_byte(byte) click to toggle source
# File lib/types/hex.rb, line 10
def fix_byte(byte)
  return byte if byte.length == 4 || byte.empty?
  remaining_bits = 4 - byte.length
  Array.new(remaining_bits) { '0' }.join('') + byte
end
to_binary() click to toggle source
# File lib/types/hex.rb, line 16
def to_binary
  binary = []
  hex.split('').map do |element|
    hexed_element = HEX_TABLE.keys.detect { |k| k.match(Regexp.new(element, Regexp::IGNORECASE)) }
    binary << fix_byte(ASMOperations::Decimal.new(HEX_TABLE[hexed_element.to_s]).binary)
  end
  binary.join('')
end