Module: Nano::Utils
Instance Method Summary collapse
-
#bin_to_hex(bin) ⇒ String
Converts a binary into a hexidecimal string.
-
#bytes_to_bin(bytes) ⇒ Binary
Converts a byte array into a binary value.
-
#bytes_to_hex(bytes) ⇒ String
Converts a byte array into hexidecimal string.
-
#hex_to_bin(hex) ⇒ Binary
Converts a hex string into a binary.
-
#hex_to_bytes(hex) ⇒ Array<Int8>
Converts a hexidecimal string into a byte array.
Instance Method Details
#bin_to_hex(bin) ⇒ String
Converts a binary into a hexidecimal string
33 34 35 |
# File 'lib/nano/utils.rb', line 33 def bin_to_hex(bin) bin.unpack('H*').first end |
#bytes_to_bin(bytes) ⇒ Binary
Converts a byte array into a binary value.
41 42 43 |
# File 'lib/nano/utils.rb', line 41 def bytes_to_bin(bytes) bytes.pack("C*") end |
#bytes_to_hex(bytes) ⇒ String
Converts a byte array into hexidecimal string
9 10 11 |
# File 'lib/nano/utils.rb', line 9 def bytes_to_hex(bytes) bytes.map { |x| "%02X" % x }.join end |
#hex_to_bin(hex) ⇒ Binary
Converts a hex string into a binary.
25 26 27 |
# File 'lib/nano/utils.rb', line 25 def hex_to_bin(hex) bytes_to_bin(hex_to_bytes(hex)) end |
#hex_to_bytes(hex) ⇒ Array<Int8>
Converts a hexidecimal string into a byte array.
17 18 19 |
# File 'lib/nano/utils.rb', line 17 def hex_to_bytes(hex) hex.scan(/../).map { |x| x.hex } end |