module Plum::BinaryString

Public Instance Methods

byteshift(count) click to toggle source

Takes from beginning and cut specified octets from this String. @param count [Integer] The amount.

# File lib/plum/binary_string.rb, line 53
def byteshift(count)
  force_encoding(Encoding::BINARY)
  slice!(0, count)
end
chunk(n) click to toggle source

Splits this String into chunks. @param n [Integer] max chunk bytesize @return [Array<String>] the slices

# File lib/plum/binary_string.rb, line 76
def chunk(n)
  res = []
  pos = 0
  lim = bytesize
  while pos < lim
    res << byteslice(pos, n)
    pos += n
  end
  res
end
each_byteslice(n) { |byteslice(pos, n)| ... } click to toggle source
# File lib/plum/binary_string.rb, line 58
def each_byteslice(n, &blk)
  if block_given?
    pos = 0
    while pos < self.bytesize
      yield byteslice(pos, n)
      pos += n
    end
  else
    Enumerator.new do |y|
      each_byteslice(n) {|ss| y << ss }
    end
    # I want to write `enum_for(__method__, n)`!
  end
end
push_uint16(val) click to toggle source

Appends a 16-bit unsigned integer to this string.

# File lib/plum/binary_string.rb, line 35
def push_uint16(val)
  self << [val].pack("n")
end
push_uint24(val) click to toggle source

Appends a 24-bit unsigned integer to this string.

# File lib/plum/binary_string.rb, line 40
def push_uint24(val)
  self << [val / 0x100, val % 0x100].pack("nC")
end
push_uint32(val) click to toggle source

Appends a 32-bit unsigned integer to this string.

# File lib/plum/binary_string.rb, line 45
def push_uint32(val)
  self << [val].pack("N")
end
push_uint8(val) click to toggle source

Appends a 8-bit unsigned integer to this string.

# File lib/plum/binary_string.rb, line 30
def push_uint8(val)
  self << val.chr
end
uint16(pos = 0) click to toggle source

Reads a 16-bit unsigned integer. @param pos [Integer] The start position to read.

# File lib/plum/binary_string.rb, line 12
def uint16(pos = 0)
  byteslice(pos, 2).unpack("n")[0]
end
uint24(pos = 0) click to toggle source

Reads a 24-bit unsigned integer. @param pos [Integer] The start position to read.

# File lib/plum/binary_string.rb, line 18
def uint24(pos = 0)
  a, b = byteslice(pos, 3).unpack("nC")
  (a * 0x100) + b
end
uint32(pos = 0) click to toggle source

Reads a 32-bit unsigned integer. @param pos [Integer] The start position to read.

# File lib/plum/binary_string.rb, line 25
def uint32(pos = 0)
  byteslice(pos, 4).unpack("N")[0]
end
uint8(pos = 0) click to toggle source

Reads a 8-bit unsigned integer. @param pos [Integer] The start position to read.

# File lib/plum/binary_string.rb, line 6
def uint8(pos = 0)
  getbyte(pos)
end