module Lignite::Bytes

Shortcut methods to convert between data and their byte representation

Public Instance Methods

bin_to_hex(bin) click to toggle source

@param bin [ByteString] “A42” @return [String] “413432”

# File lib/lignite/bytes.rb, line 44
def bin_to_hex(bin)
  bin.unpack("H*").first
end
f32(float) click to toggle source
# File lib/lignite/bytes.rb, line 16
def f32(float)
  [float].pack("e")
end
hex_to_bin(hex) click to toggle source

@param hex [String] “413432” @return [ByteString] “A42”

# File lib/lignite/bytes.rb, line 38
def hex_to_bin(hex)
  [hex].pack("H*")
end
u16(n) click to toggle source
# File lib/lignite/bytes.rb, line 8
def u16(n)
  u8(n & 0xff) + u8(n >> 8)
end
u32(n) click to toggle source
# File lib/lignite/bytes.rb, line 12
def u32(n)
  u16(n & 0xffff) + u16(n >> 16)
end
u8(n) click to toggle source
# File lib/lignite/bytes.rb, line 4
def u8(n)
  (n & 0xff).chr
end
unpack_f32(s) click to toggle source
# File lib/lignite/bytes.rb, line 32
def unpack_f32(s)
  s.unpack("e").first
end
unpack_u16(s) click to toggle source
# File lib/lignite/bytes.rb, line 24
def unpack_u16(s)
  s.unpack("S<").first
end
unpack_u32(s) click to toggle source
# File lib/lignite/bytes.rb, line 28
def unpack_u32(s)
  s.unpack("L<").first
end
unpack_u8(s) click to toggle source
# File lib/lignite/bytes.rb, line 20
def unpack_u8(s)
  s.unpack("C").first
end