Skip to content

Strings

Jeremy Phelps edited this page Oct 13, 2016 · 2 revisions

Counted Strings (Pascal-Style)

(read-counted-string size-len stream &key (byte-order :little-endian))

Reads an unsigned integer of size-len bytes from the stream in the specified byte-order, then reads a byte vector of size-len bytes. Returns two values:

  1. The byte vector
  2. The total number of bytes that were read.

(write-counted-string size-len stream buffer &key (byte-order :little-endian))

Writes the length of the buffer as an unsigned integer of size-len bytes in the specified byte-order, then writes out the buffer.

In DEFBINARY

The code generated by DEFBINARY handles conversion to and from a normal Lisp string:

(defbinary has-a-counted-string ()
   (my-string "" :type (counted-string size-len :external-format :latin1)))

Terminated Strings

(read-terminated-string stream &key (terminator (buffer 0)))

Reads a string ending in the byte sequence specified by terminator. The terminator is not included in the resulting buffer. The default is to read C-style strings, terminated by a zero.

(write-terminated-string string stream &key (terminator (buffer 0))

Writes the contents of string to the stream, then writes the terminator.

In DEFBINARY:

In DEFBINARY, the terminator is specified as an integer which must be encoded. The size of this integer in bytes is required as an additional parameter.

(defbinary has-terminated-string ()
    (ends-with-crlf "" :type (terminated-string 2 :terminator #x0d0a) :byte-order :big-endian)
    (last-line "" :type (terminated-string 1)))

Fixed Length Strings

(defbinary has-a-fixed-length-string ()
    (fixed-length "" :type (fixed-length-string 20)))