On this page:
read-bytes*
write-null-terminated-bytes
read-null-terminated-bytes
8.12

1 Bytes🔗ℹ

 (require binaryio/bytes) package: binaryio-lib

procedure

(read-bytes* len [in])  bytes?

  len : exact-nonnegative-integer?
  in : input-port? = (current-input-port)
Like read-bytes, but returns a byte string of exactly len bytes. If fewer than len bytes are available before the end of input, an exception is raised.

Examples:
> (define in (open-input-bytes #"abcde"))
> (read-bytes* 4 in)

#"abcd"

> (read-bytes* 2 in)

read-bytes*: unexpected end of input

  tried to read: 2 bytes

  available: 1 bytes

  received: #"e"

procedure

(write-null-terminated-bytes bstr    
  [out    
  start    
  end])  void?
  bstr : bytes?
  out : output-port? = (current-output-port)
  start : exact-nonnegative-integer? = 0
  end : exact-nonnegative-integer? = (bytes-length bstr)
Writes bytes to out from bstr from index start (inclusive) to end (exclusive), and then writes a null (0) byte terminator.

If bstr contains any null bytes between start and end, an error is raised.

procedure

(read-null-terminated-bytes [in])  bytes?

  in : input-port? = (current-input-port)
Reads from in until a null (0) byte is found, then returns the bytes read, excluding the null terminator. If no null terminator is found before the end of input, an error is raised.

Examples:
> (define-values (in out) (make-pipe))
> (write-null-terminated-bytes #"abcde" out)
> (read-null-terminated-bytes in)

#"abcde"