Module: MIDIMessage::TypeConversion

Defined in:
lib/midi-message/type_conversion.rb

Overview

this is a helper for converting nibbles and bytes

Class Method Summary collapse

Class Method Details

.hex_chars_to_numeric_byte_array(nibbles) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/midi-message/type_conversion.rb', line 8

def self.hex_chars_to_numeric_byte_array(nibbles)
  nibbles = nibbles.dup
  # get rid of last nibble if there's an odd number
  # it will be processed later anyway
  nibbles.slice!(nibbles.length-2, 1) if nibbles.length.odd?
  bytes = []
  while !(nibs = nibbles.slice!(0,2)).empty?
    byte = (nibs[0].hex << 4) + nibs[1].hex
    bytes << byte
  end
  bytes
end

.hex_str_to_hex_chars(str) ⇒ Object

converts a string of hex digits to bytes



32
33
34
# File 'lib/midi-message/type_conversion.rb', line 32

def self.hex_str_to_hex_chars(str)
  str.split(//)    
end

.hex_string_to_numeric_byte_array(str) ⇒ Object

convert byte str to byte array



22
23
24
25
26
27
28
29
# File 'lib/midi-message/type_conversion.rb', line 22

def self.hex_string_to_numeric_byte_array(str)
  str = str.dup
  bytes = []
  until str.eql?("")
    bytes << str.slice!(0, 2).hex
  end
  bytes
end

.numeric_byte_array_to_hex_string(bytes) ⇒ Object



36
37
38
# File 'lib/midi-message/type_conversion.rb', line 36

def self.numeric_byte_array_to_hex_string(bytes)
  bytes.map { |b| s = b.to_s(16); s.length.eql?(1) ? "0#{s}" : s }.join.upcase
end

.numeric_byte_to_hex_chars(num) ⇒ Object



40
41
42
# File 'lib/midi-message/type_conversion.rb', line 40

def self.numeric_byte_to_hex_chars(num)
  [((num & 0xF0) >> 4), (num & 0x0F)].map { |n| n.to_s(16) }      
end