std/bytes
Bytes
The Bytes class represents a contiguous array of raw binary data (bytes). It provides native methods to read/write numeric types in specific endianness, encode to Base64/Hex, and perform bitwise operations. By default, Bytes is an empty core type; you must explicitly import std/bytes to load its extension methods.
Creating & Manipulating Bytes
Construct empty byte arrays, populate them, slice, or clone. You can also easily convert strings to bytes.
@import("std/bytes")
// 1. Create a Bytes buffer with initial size of 10
val buffer = Bytes(10)
println("Size: " + buffer.size()) // 10
// 2. Append byte values (Character literals are automatically Int in Autolang)
val dynamicBytes = Bytes()
dynamicBytes.append('H')
dynamicBytes.append('e')
dynamicBytes.append('l')
dynamicBytes.append('l')
dynamicBytes.append('o')
println(dynamicBytes.toUtf8String()) // Output: Hello
// 3. String to Bytes extension
val text = "Autolang"
val rawData = text.toBytes()
println("Hex: " + rawData.toHex())Binary Serialization & Endianness
Write and read 32-bit and 64-bit integers or floating-point numbers in Little-Endian (LE) or Big-Endian (BE) format.
@import("std/bytes")
val binary = Bytes(16)
// Write a 64-bit integer in Little-Endian
binary.writeInt64LE(0, 999999)
// Write a 32-bit integer in Big-Endian at offset 8
binary.writeInt32BE(8, 1234)
println("Value at 0: " + binary.readInt64LE(0))
println("Value at 8: " + binary.readInt32BE(8))Methods Reference
Constructors
Bytes(initialSize: Int = 0): Bytes— Creates a byte array of the specified initial size.Bytes.fromString(str: String): Bytes— Constructs a byte array containing the characters of the string.
Basic Operations
append(value: Int)— Appends a byte value at the end of the array.size(): Int— Returns the total number of bytes.isEmpty(): Bool— Returnstrueif the array contains no elements.get(index: Int): Int— Retrieves the byte value at the specified index.set(index: Int, value: Int)— Sets the byte value at the specified index.clear()— Clears all elements from the array.fill(value: Int)— Fills all elements of the byte array with the specified byte value.
Copying, Slicing & Compare
slice(from: Int, to: Int): Bytes— Returns a sub-array of bytes from the specified range.copyFrom(src: Bytes, destOffset: Int, srcOffset: Int, length: Int)— Copies bytes from a source byte array to this array at specific offsets.equals(other: Bytes): Bytes— Compares this byte array with another.indexOf(byteValue: Int, fromIndex: Int = 0): Int— Returns the index of the first occurrence of the byte value, or-1if not found.
Binary Encoding & Conversions
toUtf8String(): String— Decodes the byte array as a UTF-8 string.toHex(): String— Encodes the byte array into a hex-encoded string.toBase64(): String— Encodes the byte array into a Base64-encoded string.toString(): String— Returns a string representation of the byte array.
Read & Write (Buffer IO)
readInt64LE(offset: Int): Int— Reads a 64-bit signed integer in Little-Endian format at the offset.writeInt64LE(offset: Int, value: Int)— Writes a 64-bit signed integer in Little-Endian format at the offset.readInt32BE(offset: Int): Int— Reads a 32-bit signed integer in Big-Endian format at the offset.writeInt32BE(offset: Int, value: Int)— Writes a 32-bit signed integer in Big-Endian format at the offset.readFloatLE(offset: Int): Float— Reads a 64-bit float in Little-Endian format at the offset.xorWith(other: Bytes, length: Int)— Performs an in-place bitwise XOR operation with another byte array.
Extensions (String & Int)
String.toBytes(): Bytes— Extension method to encode a String into a byte array.String.fromBytes(bytes: Bytes): String— Static extension method to decode a String from a byte array.Int.toBigEndianBytes(): Bytes— Extension method to convert an integer to a Big-Endian byte representation.
