1. Go to this page and download the library: Download zeran/byte-buffer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
zeran / byte-buffer example snippets
use Zeran\ByteBuffer\ArrayBuffer;
use Zeran\ByteBuffer\StringBuffer;
// Choose an implementation
$buffer = new ArrayBuffer();
// or
$buffer = new StringBuffer();
// Write data
$buffer->writeInt16(1000); // Write signed 16-bit integer
$buffer->writeUInt32(50000); // Write unsigned 32-bit integer
$buffer->writeFloat(123.45); // Write float
$buffer->writeUtf8String("Hello World"); // Write string
// Read data (remember to manage your position)
$buffer->setPosition(0);
$int16 = $buffer->readInt16(); // Read signed 16-bit integer
$uint32 = $buffer->readUInt32(); // Read unsigned 32-bit integer
$float = $buffer->readFloat(); // Read float
$string = $buffer->readUtf8String(11); // Read 11 bytes as UTF-8 string
// Create empty buffer
$buffer = new ArrayBuffer();
// Create from byte array
$buffer = ArrayBuffer::fromArray([0x01, 0x02, 0x03]);
// Create from string
$buffer = StringBuffer::fromString("Hello");
// Alternatively, write to buffer
$buffer = new StringBuffer();
$buffer->writeUtf8String("Hello");
// Write integers in different sizes and endianness
$buffer->writeInt8(-50); // 1-byte signed
$buffer->writeUInt8(100); // 1-byte unsigned
$buffer->writeInt16(1000); // 2-byte signed (little-endian)
$buffer->writeInt16BE(1000); // 2-byte signed (big-endian)
$buffer->writeUInt16(1000); // 2-byte unsigned (little-endian)
$buffer->writeUInt16BE(1000); // 2-byte unsigned (big-endian)
$buffer->writeInt32(-123456); // 4-byte signed (little-endian)
$buffer->writeUInt32BE(123456); // 4-byte unsigned (big-endian)
$buffer->writeInt64(-9876543210); // 8-byte signed (little-endian)
// Generic write methods with custom size
$buffer->writeInt(-1000, 3); // 3-byte signed (little-endian)
$buffer->writeUIntBE(100000, 3); // 3-byte unsigned (big-endian)
// Reading integers
$buffer->setPosition(0);
$int8 = $buffer->readInt8();
$uint8 = $buffer->readUInt8();
$int16 = $buffer->readInt16();
$int16be = $buffer->readInt16BE();
// ...and so on
// Get current position
$position = $buffer->getPosition();
// Set position
$buffer->setPosition(4);
// Get total buffer size
$size = $buffer->size();
// Check remaining bytes
$remaining = $buffer->remains();
// Check if there are more bytes to read
$hasMore = $buffer->isMore();
// Trim the buffer
$buffer->ltrim(); // Remove bytes before current position
$buffer->rtrim(); // Remove bytes after current position
// Clear the buffer
$buffer->clear();
// Get raw buffer
$rawBuffer = $buffer->getBuffer(); // Returns string
// Convert buffer to byte array
$byteArray = $buffer->toBytesArray();
// String representation (hex format)
$hexString = $buffer->__toString(); // e.g., "01 02 03 04"
// Export as string
$encodedString = $buffer->encode();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.