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<Integer>
Converts an array of hex nibble strings to numeric bytes eg [“9”, “0”, “5”, “0”, “4”, “0”] => [0x90, 0x50, 0x40].
-
#hex_str_to_hex_chars(string) ⇒ Array<String>
Converts a string of hex digits to string nibbles eg “905040” => [“9”, “0”, “5”, “0”, “4”, “0”].
-
#hex_str_to_numeric_bytes(string) ⇒ Array<String>
Converts a string of hex digits to numeric bytes eg “905040” => [0x90, 0x50, 0x40].
-
#hex_str_to_numeric_nibbles(string) ⇒ Array<String>
Converts a string of hex digits to numeric nibbles eg “905040” => [0x9, 0x0, 0x5, 0x0, 0x4, 0x0].
-
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Converts a numeric byte to an array of hex nibble strings eg 0x90 => [“9”, “0”].
-
#numeric_byte_to_numeric_nibbles(num) ⇒ Array<String>
Converts a numeric byte to an array of numeric nibbles eg 0x90 => [0x9, 0x0].
-
#numeric_bytes_to_numeric_nibbles(bytes) ⇒ Array<String>
Converts an array bytes to an array of nibbles eg [0x90, 0x50, 0x40] => [0x9, 0x0, 0x5, 0x0, 0x4, 0x0].
Instance Method Details
#hex_chars_to_numeric_bytes(nibbles) ⇒ Array<Integer>
Converts an array of hex nibble strings to numeric bytes eg [“9”, “0”, “5”, “0”, “4”, “0”] => [0x90, 0x50, 0x40]
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/nibbler/type_conversion.rb', line 12 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 = [] until (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 eg “905040” => [“9”, “0”, “5”, “0”, “4”, “0”]
29 30 31 |
# File 'lib/nibbler/type_conversion.rb', line 29 def hex_str_to_hex_chars(string) string.split(//) end |
#hex_str_to_numeric_bytes(string) ⇒ Array<String>
Converts a string of hex digits to numeric bytes eg “905040” => [0x90, 0x50, 0x40]
46 47 48 49 |
# File 'lib/nibbler/type_conversion.rb', line 46 def hex_str_to_numeric_bytes(string) chars = hex_str_to_hex_chars(string) hex_chars_to_numeric_bytes(chars) end |
#hex_str_to_numeric_nibbles(string) ⇒ Array<String>
Converts a string of hex digits to numeric nibbles eg “905040” => [0x9, 0x0, 0x5, 0x0, 0x4, 0x0]
37 38 39 40 |
# File 'lib/nibbler/type_conversion.rb', line 37 def hex_str_to_numeric_nibbles(string) bytes = hex_str_to_numeric_bytes(string) numeric_bytes_to_numeric_nibbles(bytes) end |
#numeric_byte_to_hex_chars(num) ⇒ Array<String>
Converts a numeric byte to an array of hex nibble strings eg 0x90 => [“9”, “0”]
62 63 64 65 |
# File 'lib/nibbler/type_conversion.rb', line 62 def numeric_byte_to_hex_chars(num) nibbles = numeric_byte_to_numeric_nibbles(num) nibbles.map { |n| n.to_s(16) } end |
#numeric_byte_to_numeric_nibbles(num) ⇒ Array<String>
Converts a numeric byte to an array of numeric nibbles eg 0x90 => [0x9, 0x0]
70 71 72 |
# File 'lib/nibbler/type_conversion.rb', line 70 def numeric_byte_to_numeric_nibbles(num) [((num & 0xF0) >> 4), (num & 0x0F)] end |
#numeric_bytes_to_numeric_nibbles(bytes) ⇒ Array<String>
Converts an array bytes to an array of nibbles eg [0x90, 0x50, 0x40] => [0x9, 0x0, 0x5, 0x0, 0x4, 0x0]
55 56 57 |
# File 'lib/nibbler/type_conversion.rb', line 55 def numeric_bytes_to_numeric_nibbles(bytes) bytes.map { |byte| numeric_byte_to_numeric_nibbles(byte) }.flatten end |