Module: Lignite::Bytes

Overview

Shortcut methods to convert between data and their byte representation

Instance Method Summary collapse

Instance Method Details

#bin_to_hex(bin) ⇒ String

Returns “413432”.

Parameters:

Returns:

  • (String)

    “413432”



44
45
46
# File 'lib/lignite/bytes.rb', line 44

def bin_to_hex(bin)
  bin.unpack("H*").first
end

#f32(float) ⇒ Object



16
17
18
# File 'lib/lignite/bytes.rb', line 16

def f32(float)
  [float].pack("e")
end

#hex_to_bin(hex) ⇒ ByteString

Returns “A42”.

Parameters:

  • hex (String)

    “413432”

Returns:



38
39
40
# File 'lib/lignite/bytes.rb', line 38

def hex_to_bin(hex)
  [hex].pack("H*")
end

#u16(n) ⇒ Object



8
9
10
# File 'lib/lignite/bytes.rb', line 8

def u16(n)
  u8(n & 0xff) + u8(n >> 8)
end

#u32(n) ⇒ Object



12
13
14
# File 'lib/lignite/bytes.rb', line 12

def u32(n)
  u16(n & 0xffff) + u16(n >> 16)
end

#u8(n) ⇒ Object



4
5
6
# File 'lib/lignite/bytes.rb', line 4

def u8(n)
  (n & 0xff).chr
end

#unpack_f32(s) ⇒ Object



32
33
34
# File 'lib/lignite/bytes.rb', line 32

def unpack_f32(s)
  s.unpack("e").first
end

#unpack_u16(s) ⇒ Object



24
25
26
# File 'lib/lignite/bytes.rb', line 24

def unpack_u16(s)
  s.unpack("S<").first
end

#unpack_u32(s) ⇒ Object



28
29
30
# File 'lib/lignite/bytes.rb', line 28

def unpack_u32(s)
  s.unpack("L<").first
end

#unpack_u8(s) ⇒ Object



20
21
22
# File 'lib/lignite/bytes.rb', line 20

def unpack_u8(s)
  s.unpack("C").first
end