Module: Utils::ByteUtils

Extended by:
ByteUtils
Included in:
ByteUtils
Defined in:
lib/utils/byte_utils.rb

Instance Method Summary collapse

Instance Method Details

#byte_array_to_hex(byte_array) ⇒ String

Parameters:

  • byte_array (Array)

Returns:

  • (String)


26
27
28
29
# File 'lib/utils/byte_utils.rb', line 26

def byte_array_to_hex(byte_array)
  # byte_array.pack("C*").unpack1("H*")
  byte_array.pack("C*").unpack("H*").first
end

#byte_array_to_string(byte_array) ⇒ String

Parameters:

  • byte_array (Array)

Returns:

  • (String)


39
40
41
# File 'lib/utils/byte_utils.rb', line 39

def byte_array_to_string(byte_array)
  byte_array.pack("C*")
end

#hex_from_little_endian_to_big_endian(hex_str) ⇒ String

Parameters:

  • hex_str (String)

Returns:

  • (String)


82
83
84
85
# File 'lib/utils/byte_utils.rb', line 82

def hex_from_little_endian_to_big_endian(hex_str)
  # [hex_str].pack("H*").unpack('N*').pack('V*').unpack1('H*')
  [hex_str].pack("H*").unpack('N*').pack('V*').unpack('H*').first
end

#hex_to_byte_array(hex_str) ⇒ Array

Parameters:

  • hex_str (String)

Returns:

  • (Array)


20
21
22
# File 'lib/utils/byte_utils.rb', line 20

def hex_to_byte_array(hex_str)
  [hex_str].pack("H*").unpack("C*")
end

#hex_to_i32_value(hex_str) ⇒ Object



87
88
89
# File 'lib/utils/byte_utils.rb', line 87

def hex_to_i32_value(hex_str)
  [hex_str].pack("H*").unpack("l*").first
end

#hex_to_i64_value(hex_str) ⇒ Object



91
92
93
# File 'lib/utils/byte_utils.rb', line 91

def hex_to_i64_value(hex_str)
  [hex_str].pack("H*").unpack("q*").first
end

#hex_to_integer(hex_str) ⇒ Integer

Parameters:

  • hex_str (String)

Returns:

  • (Integer)


51
52
53
# File 'lib/utils/byte_utils.rb', line 51

def hex_to_integer(hex_str)
  [hex_str].pack("H*").unpack("l").first
end

#hex_to_string(hex_str) ⇒ String

Parameters:

  • hex_str (String)

Returns:

  • (String)


14
15
16
# File 'lib/utils/byte_utils.rb', line 14

def hex_to_string(hex_str)
  [hex_str].pack("H*")
end

#hex_to_u32_value(hex_str) ⇒ Object



99
100
101
# File 'lib/utils/byte_utils.rb', line 99

def hex_to_u32_value(hex_str)
  [hex_str].pack("H*").unpack("L*").first
end

#hex_to_u64_value(hex_str) ⇒ Object



103
104
105
# File 'lib/utils/byte_utils.rb', line 103

def hex_to_u64_value(hex_str)
  [hex_str].pack("H*").unpack("Q*").first
end

#hex_to_u8_value(hex_str) ⇒ Object



95
96
97
# File 'lib/utils/byte_utils.rb', line 95

def hex_to_u8_value(hex_str)
  [hex_str].pack("H*").unpack("C*").first
end

#integer_to_hex(n) ⇒ String

Parameters:

  • n (Integer)

Returns:

  • (String)


45
46
47
# File 'lib/utils/byte_utils.rb', line 45

def integer_to_hex(n)
  [n].pack("l<*").unpack("H*").first
end

#string_to_byte_array(str) ⇒ Array

Parameters:

  • str (String)

Returns:

  • (Array)


33
34
35
# File 'lib/utils/byte_utils.rb', line 33

def string_to_byte_array(str)
  str.unpack("C*")
end

#string_to_hex(str) ⇒ String

Parameters:

  • str (String)

Returns:

  • (String)


7
8
9
10
# File 'lib/utils/byte_utils.rb', line 7

def string_to_hex(str)
  # str.unpack1("H*")
  str.unpack("H*").first
end

#to_i32(n) ⇒ Object



55
56
57
58
59
# File 'lib/utils/byte_utils.rb', line 55

def to_i32(n)
  # [value].pack("l<*").unpack("C*")
  [n].pack("l<*").unpack("H*").first
  # [n].pack("l<*").unpack1("H*")
end

#to_i64(n) ⇒ Object



61
62
63
64
65
# File 'lib/utils/byte_utils.rb', line 61

def to_i64(n)
  # [value].pack("l<*").unpack("C*")
  [n].pack("q<*").unpack("H*").first
  # [n].pack("l<*").unpack1("H*")
end

#to_u32(n) ⇒ Object



70
71
72
73
# File 'lib/utils/byte_utils.rb', line 70

def to_u32(n)
  [n].pack("L<*").unpack("H*").first
  # [n].pack("L<*").unpack1("H*")
end

#to_u64(n) ⇒ String

Parameters:

  • n (Integer)

Returns:

  • (String)


76
77
78
# File 'lib/utils/byte_utils.rb', line 76

def to_u64(n)
  [n].pack("Q<*").unpack("H*").first
end

#to_u8(n) ⇒ Object



67
68
69
# File 'lib/utils/byte_utils.rb', line 67

def to_u8(n)
  [n].pack("C").unpack1("H*")
end