std/bytes
Binary Data (Bytes)
The Bytes class in std/bytes manages contiguous binary memory buffers. It provides utilities for Hex and Base64 encoding/decoding, endianness-aware integer reading and writing, and memory-safe binary slicing.
Initialization & Encoding Conversions
Create empty byte buffers with an initial capacity or parse them directly from Hex or Base64 encoded strings.
@import("std/bytes")
// 1. Parse from Hex string
val signatureBytes = Bytes.fromHex("deadbeef")
println("Hex bytes size: " + signatureBytes.size() + ", hex: " + signatureBytes.toHex())
// 2. Parse from Base64 string
val payload = Bytes.fromBase64("SGVsbG8gV29ybGQ=")
println("Decoded: " + payload.decodeToString())
println("Base64: " + payload.encodeBase64())
// 3. Encode string to byte array
val rawBytes = "Autolang".encodeToByteArray()
println("Raw bytes size: " + rawBytes.size())Buffer Operations & Slicing
Append byte values, update items by index, extract sub-buffers, and export buffers to integer arrays.
@import("std/bytes")
val buffer = Bytes()
buffer.append(10)
buffer.append(20)
buffer.append(30)
buffer.append(40)
println("Buffer size: " + buffer.size())
println("Byte at 0: " + buffer.get(0))
buffer.set(1, 99)
val subBuffer = buffer.slice(1, 3) // Extracts bytes at indices 1 and 2
println("Sub-buffer size: " + subBuffer.size())
val intList = buffer.toByteArray()
println("First byte in array: " + intList[0])Endianness & Integer I/O
Read and write 32-bit and 64-bit integers with explicit Little-Endian (LE) and Big-Endian (BE) byte orders for binary wire formats and protocol packets.
@import("std/bytes")
val packet = Bytes(16)
// Write 64-bit integer in Little-Endian format
packet.writeInt64LE(0, 990001)
// Write 32-bit integer in Big-Endian format at offset 8
packet.writeInt32BE(8, 443)
println("Read Int64 LE: " + packet.readInt64LE(0))
println("Read Int32 BE: " + packet.readInt32BE(8))Method Reference
Constructors & Static Parsing
Bytes(capacity: Int = 0): Bytes: Initializes a buffer with the specified initial capacity.Bytes.fromHex(hex: String): Bytes: Parses a hexadecimal string into a byte buffer.Bytes.fromBase64(b64: String): Bytes: Decodes a Base64 string into a byte buffer.
Buffer Instance Methods
size(): Int: Returns the current buffer size in bytes.get(index: Int): Int: Reads the byte at the specified index.set(index: Int, byteValue: Int): Overwrites the byte at the specified index.append(byteValue: Int): Appends a byte to the end of the buffer.slice(start: Int, end: Int): Bytes: Extracts a sub-buffer slice.toHex(): String: Exports the buffer as a hexadecimal string.encodeBase64(): String: Encodes the buffer to a Base64 string.decodeToString(): String: Decodes binary content into a UTF-8 string.toByteArray(): Array<Int>: Exports all bytes to an array of integers.writeInt64LE(offset: Int, value: Int)/readInt64LE(offset: Int): Int: Writes/reads a 64-bit integer in Little-Endian byte order.writeInt32BE(offset: Int, value: Int)/readInt32BE(offset: Int): Int: Writes/reads a 32-bit integer in Big-Endian byte order.