Sha256: 88ff325a08154cef9697abe62be532213cc40064a23f9e587a0a7815de3a736e

Contents?: true

Size: 810 Bytes

Versions: 3

Compression:

Stored size: 810 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

    # @param hex [String] "413432"
    # @return [ByteString] "A42"
    def hex_to_bin(hex)
      [hex].pack("H*")
    end

    # @param bin [ByteString] "A42"
    # @return [String] "413432"
    def bin_to_hex(bin)
      bin.unpack("H*").first
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lignite-0.6.0 lib/lignite/bytes.rb
lignite-0.5.0 lib/lignite/bytes.rb
lignite-0.4.0 lib/lignite/bytes.rb