def bytes_from_data(data, offset: 0, limit: nil, range: nil)
      raise ArgumentError, "Data is missing" if !data
      if offset == 0 && limit == nil && range == nil
        return data.bytes
      end
      if range
        offset = range.begin
        limit  = range.size
      end
      bytes = []
      data.each_byte do |byte|
        if offset > 0
          offset -= 1
        else
          if !limit || limit > 0
            bytes << byte
            limit -= 1 if limit
          else
            break
          end
        end
      end
      bytes
    end
def bytes_from_data(data, offset: 0, limit: nil, range: nil)
      raise ArgumentError, "Data is missing" if !data
      if offset == 0 && limit == nil && range == nil
        return data.bytes
      end
      if range
        offset = range.begin
        limit  = range.size
      end
      bytes = []
      data.each_byte do |byte|
        if offset > 0
          offset -= 1
        else
          if !limit || limit > 0
            bytes << byte
            limit -= 1 if limit
          else
            break
          end
        end
      end
      bytes
    end
Converts a binary string to an array of bytes (list of integers).
 Returns a much more efficient slice of bytes if offset/limit or
 range are specified. That is, avoids converting the entire buffer to byte array.

 Note 1: if range is specified, it takes precedence over offset/limit.

 Note 2: byteslice(...).bytes is less efficient as it creates
         an intermediate shorter string.