Module: Nano::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/nano/utils.rb

Instance Method Summary collapse

Instance Method Details

#bin_to_hex(bin) ⇒ String

Converts a binary into a hexidecimal string

Parameters:

  • bin (Binary)

    The binary value.

Returns:

  • (String)

    The hexidecimal string representing the binary value.



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.

Parameters:

  • bytes (Array<Int8>)

    The byte array of integers.

Returns:

  • (Binary)

    The binary value representing the byte array.



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

Parameters:

  • bytes (Array<Int8>)

    An array of integers representing bytes.

Returns:

  • (String)

    A hexidecimal byte 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.

Parameters:

  • hex (String)

    A hexidecimal string of arbitrary length.

Returns:

  • (Binary)

    A binary representing the hex string



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.

Parameters:

  • hex (String)

    A hexidecimal string of arbitrary length.

Returns:

  • (Array<Int8>)

    An array of integers representing the hex string.



17
18
19
# File 'lib/nano/utils.rb', line 17

def hex_to_bytes(hex)
  hex.scan(/../).map { |x| x.hex }
end