# Binary Data (ByteBuffer)
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.
## Initialization
A `ByteBuffer` can be initialized from strings, hex, or by setting its length directly.
```pascal
var buffer: ByteBuffer;
buffer.SetLength(1024); // Allocate 1KB
buffer := ByteBuffer('Hello');
PrintLn(buffer.Length.ToString);
// OUTPUT
// 5
```
## Accessing Data
You can access individual bytes using `GetByte` and `SetByte` methods.
```pascal
var buffer: ByteBuffer;
buffer.SetLength(2);
buffer.SetByte(0, $FF);
var b := buffer.GetByte(0);
PrintLn(b.ToString);
// OUTPUT
// 255
```
## Methods
`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.
```pascal
var buf: ByteBuffer;
buf.SetLength(4);
buf.SetInt32(0, 123456);
var i := buf.GetInt32(0);
PrintLn(i.ToString);
// OUTPUT
// 123456
```
## ByteBuffer vs DataStrings
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.
* **ByteBuffer**: Preferred for all raw binary manipulation and multi-byte operations.
* **DataStrings**: Used for interoperability with legacy APIs or functions that expect a binary stream in string form.
To convert a `ByteBuffer` to a DataString, use `.ToDataString`. To convert back, use the `ByteBuffer()` constructor or `.AssignDataString`.
```pascal
var buffer := ByteBuffer('Hello');
var binStr := buffer.ToDataString; // binStr is a DataString
PrintLn(binStr);
// OUTPUT
// Hello
```
:::info
### Related Reference
* **[Encoding Reference](/ref/encoding)** - Base64, Hex, and URL encoding.
* **[Crypto Reference](/ref/crypto)** - Using ByteBuffers for hashing and encryption.
:::
Binary Data (ByteBuffer)
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.
Initialization
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);
Result
5
Accessing Data
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);
Result
255
Methods
ByteBuffer provides many methods for reading and writing multi-byte values.
GetInt32(offset) / SetInt32(offset, value): Standard 32-bit integers.
var buf:ByteBuffer;
buf.SetLength(4);
buf.SetInt32(0,123456);var i := buf.GetInt32(0);
PrintLn(i.ToString);
Result
123456
ByteBuffer vs DataStrings
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.
ByteBuffer: Preferred for all raw binary manipulation and multi-byte operations.
DataStrings: Used for interoperability with legacy APIs or functions that expect a binary stream in string form.
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);