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_hex_64(byte_array) ⇒ Object



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

def byte_array_to_hex_64(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)


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

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)


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

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



101
102
103
# File 'lib/utils/byte_utils.rb', line 101

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

#hex_to_i64_value(hex_str) ⇒ Object



105
106
107
# File 'lib/utils/byte_utils.rb', line 105

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)


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

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



113
114
115
# File 'lib/utils/byte_utils.rb', line 113

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

#hex_to_u512_value(hex_str) ⇒ Object



120
121
122
# File 'lib/utils/byte_utils.rb', line 120

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

#hex_to_u64_value(hex_str) ⇒ Object



117
118
119
# File 'lib/utils/byte_utils.rb', line 117

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

#hex_to_u8_value(hex_str) ⇒ Object



109
110
111
# File 'lib/utils/byte_utils.rb', line 109

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)


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

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

#string_to_byte_array(str) ⇒ Array

Parameters:

  • str (String)

Returns:

  • (Array)


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

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_byte_array(num) ⇒ Object

Returns byte array containing two’s complement representation of Bignum/Fixnum.

Returns:

  • byte array containing two’s complement representation of Bignum/Fixnum



125
126
127
128
129
130
131
132
133
# File 'lib/utils/byte_utils.rb', line 125

def to_byte_array(num)
  result = []
  begin
    result << (num & 0xff)
    num >>= 8
  end until (num == 0 || num == -1) && (result.last[7] == num[7])
  # result.reverse
  result
end

#to_i32(n) ⇒ Object



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

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



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

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



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

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

#to_u512(n) ⇒ String

Parameters:

  • n (Integer)

Returns:

  • (String)


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

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

#to_u64(n) ⇒ String

Parameters:

  • n (Integer)

Returns:

  • (String)


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

def to_u64(n)
  # bytes = Utils::ByteUtils.byte_array_to_hex(to_byte_array(n)) + "0000"
  # puts "bytes: #{bytes}"
  # puts "bytes_conversion: #{[bytes].pack("H*").unpack("C*")}"
  [n].pack("Q<*").unpack("H*").first
end

#to_u8(n) ⇒ Object



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

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