Module: MIDIMessage::TypeConversion
- Extended by:
- TypeConversion
- Included in:
- TypeConversion
- Defined in:
- lib/midi-message/type_conversion.rb
Overview
Helper for converting nibbles and bytes
Instance Method Summary collapse
-
#hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Fixnum] An array of numeric bytes eg [0x90, 0x40]
Convert an array of hex nibbles to an array of numeric bytes eg [“9”, “0”, “4”, “0”] to [0x90, 0x40].
-
#hex_str_to_hex_chars(string) ⇒ Array<String>
Convert a string of hex digits to an array of nibbles eg.
-
#hex_string_to_numeric_byte_array(string) ⇒ Array<Fixnum>
Convert byte string to an array of numeric bytes eg.
-
#numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to a string of hex digits eg.
-
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex chars eg 0x90 to [“9”, “0”].
-
#numeric_byte_to_nibbles(num) ⇒ Array<Fixnum>
Convert a numeric byte to nibbles eg 0x90 to [0x9, 0x0].
Instance Method Details
#hex_chars_to_numeric_byte_array(nibbles) ⇒ Array<Fixnum] An array of numeric bytes eg [0x90, 0x40]
Convert an array of hex nibbles to an array of numeric bytes eg [“9”, “0”, “4”, “0”] to [0x90, 0x40]
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/midi-message/type_conversion.rb', line 13 def hex_chars_to_numeric_byte_array(nibbles) nibbles = nibbles.dup # Don't mess with the input # 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(string) ⇒ Array<String>
Convert a string of hex digits to an array of nibbles eg. “904040” to [“9”, “0”, “4”, “0”, “4”, “0”]
43 44 45 |
# File 'lib/midi-message/type_conversion.rb', line 43 def hex_str_to_hex_chars(string) string.split(//) end |
#hex_string_to_numeric_byte_array(string) ⇒ Array<Fixnum>
Convert byte string to an array of numeric bytes eg. “904040” to [0x90, 0x40, 0x40]
30 31 32 33 34 35 36 37 |
# File 'lib/midi-message/type_conversion.rb', line 30 def hex_string_to_numeric_byte_array(string) string = string.dup bytes = [] until string.length == 0 bytes << string.slice!(0, 2).hex end bytes end |
#numeric_byte_array_to_hex_string(bytes) ⇒ String
Convert an array of numeric bytes to a string of hex digits eg. [0x90, 0x40, 0x40] to “904040”
51 52 53 54 55 56 57 58 |
# File 'lib/midi-message/type_conversion.rb', line 51 def numeric_byte_array_to_hex_string(bytes) string_bytes = bytes.map do |byte| string = byte.to_s(16) string = "0#{string}" if string.length == 1 string end string_bytes.join.upcase end |
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Convert a numeric byte to hex chars eg 0x90 to [“9”, “0”]
64 65 66 67 |
# File 'lib/midi-message/type_conversion.rb', line 64 def numeric_byte_to_hex_chars(num) nibbles = numeric_byte_to_nibbles(num) nibbles.map { |n| n.to_s(16) } end |
#numeric_byte_to_nibbles(num) ⇒ Array<Fixnum>
Convert a numeric byte to nibbles eg 0x90 to [0x9, 0x0]
73 74 75 |
# File 'lib/midi-message/type_conversion.rb', line 73 def numeric_byte_to_nibbles(num) [((num & 0xF0) >> 4), (num & 0x0F)] end |