Module: Nibbler::TypeConversion
Overview
A helper for converting between different types of nibbles and bytes
Instance Method Summary collapse
-
#hex_chars_to_numeric_bytes(nibbles) ⇒ Array<Fixnum>
Converts an array of hex nibble strings to numeric bytes.
-
#hex_str_to_hex_chars(string) ⇒ Array<String>
Converts a string of hex digits to string nibbles.
-
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Converts a numeric byte to an array of hex nibble strings.
Instance Method Details
#hex_chars_to_numeric_bytes(nibbles) ⇒ Array<Fixnum>
Converts an array of hex nibble strings to numeric bytes
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/nibbler/type_conversion.rb', line 11 def hex_chars_to_numeric_bytes(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(string) ⇒ Array<String>
Converts a string of hex digits to string nibbles
27 28 29 |
# File 'lib/nibbler/type_conversion.rb', line 27 def hex_str_to_hex_chars(string) string.split(//) end |
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Converts a numeric byte to an array of hex nibble strings
34 35 36 |
# File 'lib/nibbler/type_conversion.rb', line 34 def numeric_byte_to_hex_chars(num) [((num & 0xF0) >> 4), (num & 0x0F)].map { |n| n.to_s(16) } end |