bytes Module
The bytes module provides manipulation and conversions for raw byte buffers, binary encodings, and hex representations.
Importing
djazair
use bytes
API Reference
| Function | Description |
|---|---|
bytes.alloc(size) | Allocates a zeroed byte buffer of given size. |
bytes.fromString(str) | Converts a UTF-8 string into an array of byte integers (0-255). |
bytes.toString(buf) | Converts an array of byte integers back into a UTF-8 string. |
bytes.hexToBytes(hexStr) | Decodes a hexadecimal string (e.g. "48656c6c6f") into bytes. |
bytes.bytesToHex(buf) | Encodes a byte array into a hex string. |
bytes.slice(buf, start, end) | Slices a subarray of bytes. |
bytes.indexOf(buf, byteVal) | Finds index of first occurrence of byte (-1 if not found). |
bytes.contains(buf, byteVal) | Returns True if byte is present. |
bytes.concat(bufA, bufB) | Concatenates two byte arrays. |
Example Usage
djazair
use bytes
# Allocate or decode a byte buffer from hex
let buf = bytes.hexToBytes("deadbeef")
# Convert to hex string
print(bytes.bytesToHex(buf)) # => "deadbeef"
# Roundtrip from string
let msg = "Hello Djazair"
let encoded = bytes.fromString(msg)
print("Bytes length: ${encoded.length()}") # => 13
print(bytes.toString(encoded)) # => "Hello Djazair"