The ByteBuffer type is a specialized dynamic array of bytes (array of Byte) optimized for binary data manipulation. It is the primary type for handling network packets, file contents, and cryptographic blobs.
A ByteBuffer can be initialized from strings, hex, or by setting its length directly.
var buffer: ByteBuffer;
buffer.SetLength(1024); // Allocate 1KB
buffer := ByteBuffer('Hello');
PrintLn(buffer.Length.ToString); 5
You can access individual bytes using GetByte and SetByte methods.
var buffer: ByteBuffer;
buffer.SetLength(2);
buffer.SetByte(0, $FF);
var b := buffer.GetByte(0);
PrintLn(b.ToString); 255
ByteBuffer provides many methods for reading and writing multi-byte values.
GetInt32(offset) / SetInt32(offset, value): Standard 32-bit integers.GetFloat(offset): 64-bit doubles.GetWord(offset): 16-bit unsigned integers.ToHexString: Returns a hex string representation.Base64Encode / Base64Decode: Conversion to/from Base64.var buf: ByteBuffer;
buf.SetLength(4);
buf.SetInt32(0, 123456);
var i := buf.GetInt32(0);
PrintLn(i.ToString); 123456
In DWScript, DataStrings are often used to represent raw data in a string format. However, these are UTF-16 strings where many functions only write or read the lower byte of each character.
To convert a ByteBuffer to a DataString, use .ToDataString. To convert back, use the ByteBuffer() constructor or .AssignDataString.
var buffer := ByteBuffer('Hello');
var binStr := buffer.ToDataString; // binStr is a DataString
PrintLn(binStr); Hello