Module: MIDICommunications::TypeConversion

Extended by:
TypeConversion
Included in:
TypeConversion
Defined in:
lib/midi-communications/type_conversion.rb

Overview

Utility methods for converting between MIDI data formats.

API:

  • public

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.numeric_byte_array_to_hex_string(bytes) ⇒ String

Converts an array of numeric bytes to a hex string.

Every byte becomes exactly two characters. Without the padding a byte below 0x10 produces one, and the string can no longer be read back as bytes: [0x90, 0x0A, 0x64] came out as "90a64", which is five characters and describes no MIDI message at all.

Examples:

TypeConversion.numeric_byte_array_to_hex_string([0x90, 0x40, 0x40])
# => "904040"

A byte below 0x10 still takes two characters

TypeConversion.numeric_byte_array_to_hex_string([0x90, 0x0A, 0x64])
# => "900A64"

Parameters:

  • array of numeric bytes (e.g., [0x90, 0x40, 0x40])

Returns:

  • hex string representation (e.g., "904040")

API:

  • public



25
26
27
# File 'lib/midi-communications/type_conversion.rb', line 25

def numeric_byte_array_to_hex_string(bytes)
  bytes.map { |byte| format('%02X', byte) }.join
end

Instance Method Details

#numeric_byte_array_to_hex_string(bytes) ⇒ String

Converts an array of numeric bytes to a hex string.

Every byte becomes exactly two characters. Without the padding a byte below 0x10 produces one, and the string can no longer be read back as bytes: [0x90, 0x0A, 0x64] came out as "90a64", which is five characters and describes no MIDI message at all.

Examples:

TypeConversion.numeric_byte_array_to_hex_string([0x90, 0x40, 0x40])
# => "904040"

A byte below 0x10 still takes two characters

TypeConversion.numeric_byte_array_to_hex_string([0x90, 0x0A, 0x64])
# => "900A64"

Parameters:

  • array of numeric bytes (e.g., [0x90, 0x40, 0x40])

Returns:

  • hex string representation (e.g., "904040")

API:

  • public



25
26
27
# File 'lib/midi-communications/type_conversion.rb', line 25

def numeric_byte_array_to_hex_string(bytes)
  bytes.map { |byte| format('%02X', byte) }.join
end