Bytes and strings
The bytes
type
represents raw bytes, typically from protocol data. Literals for bytes
are
written with prefix b
, e.g., b"\x00byteData\x01"
.
The string
type
represents text in a given character set.
Conversion between bytes
and string
are always explicit, via bytes
'
decode
method
or string
's
encode
,
e.g.,
global my_bytes = b"abc";
global my_string = "abc";
global my_other_string = my_bytes.decode(); # Default: UTF-8.
print my_bytes, my_string, my_other_string;
bytes
can be iterated over.
for (byte in b"abc") {
print byte;
}
Use the format operator
%
to compute a string representation of Spicy values. Format strings roughly follow the
POSIX format string
API.
global n = 4711;
global s = "%d" % n;
The format operator can be used to format multiple values.
global start = 0;
global end = 1024;
print "[%d, %d)" % (start, end);