Sha256: 9528d59d9875da5f12b9ab148d51993f8a02292c1a7f3bb238c3f196d5307194

Contents?: true

Size: 611 Bytes

Versions: 2

Compression:

Stored size: 611 Bytes

Contents

module Lignite
  # Shortcut methods to convert between data and their byte representation
  module Bytes
    def u8(n)
      (n & 0xff).chr
    end

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

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

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

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

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

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

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

    def hexdump(s)
      s.unpack("H*").first
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lignite-0.3.0 lib/lignite/bytes.rb
lignite-0.2.0 lib/lignite/bytes.rb