sys.Buffer(var, [encoding]) constructor

The Buffer constructor returns a Buffer value representing a sequence of raw bytes in memory, initialized with the var argument. The Buffer value can then be accessed, modified, resized, converted from/to string, compressed, decoded...

Prefer to use Buffer instead of strings when manipulating binary data. Remember that Luart strings are UTF8 encoded.

Parameters

var

A variable used to initialize the Buffer. Only number, string and table values are permitted with the following behavior :

  • number : Initialize the Buffer with the specified size. All bytes are initialized to zero.
  • string : Initialize the Buffer with the specified string. An optional encoding argument can specify how to decode the string (see below)
  • table : Initialize the Buffer with the specified table. The table must contain only sequential byte values (for example { 20, 33, 75 })

encoding

An optional string used to specify the decoding method when initializing the Buffer with a string value :

  • "utf8" : Initialize the Buffer with a UTF8 encoded string, or as raw binary data (default)
  • "unicode" : Initialize the Buffer with an UNICODE (UCS-2 LE) encoded string
  • "base64" : Initialize the Buffer with a base64 encoded string
  • "hex" : Initialize the Buffer with a hexadecimal encoded string

Example

-- initialize with a table, sequence of bytes values local b = sys.Buffer( {0x41, 0, 0x42, 0, 0x43, 0} ) -- decode the Buffer as a unicode string and prints it (outputs "ABC") print(b:encode("unicode"))