APIByte sequences

Byte sequences

The Java implementation uses the Bytes class for byte sequences. A Bytes object is basically a slice of a byte[] array (byte buffer).

The JavaScript implementation uses Uint8Array instances as byte sequences. An Uint8Array is basically a slice of an ArrayBuffer (byte buffer).

The Perl implementation uses Perl byte strings (strings without the UTF-8 flag) as byte sequences.

Creation

Bytes bytes = new Bytes(32);
Bytes bytes = new Bytes(new byte[]{67, 78});
Bytes bytes = new Bytes(byteArray, offset, length);
var bytes = new Uint8Array(32);
var bytes = new Uint8Array([67, 78]);
var bytes = new Uint8Array(arrayBuffer, offset, length);
my $bytes = 'some bytes';

Creates a byte string.

Bytes randomBytes = Condensation.randomBytes(16);
var randomBytes = cn.randomBytes(16);
my $randomBytes = CN->randomBytes(16);

Creates a sequence of 16 random bytes.

Bytes randomBytes = Condensation.randomKey();
var randomBytes = cn.randomKey();
my $randomBytes = CN->randomKey;

Creates a random AES key, i.e. a sequence of 32 random bytes.

Slicing

Bytes slice = bytes.slice(10, 4);
var slice = cn.slice(bytes, 10, 4);
my $slice = substr($bytes, 10, 4);

Creates a slice of length 4 starting at byte 10. The slice points to the same byte buffer – the bytes are not copied.

Creates a slice of length 4 starting at byte 10.

Concatenation

Bytes concatenated = bytes1.concatenate(bytes2);
Bytes concatenated = Bytes.concatenate(bytesIterable);
var concatenated = cn.concatenate(bytes1, bytes2);
my $concatenated = $bytes1.$bytes2;

Concatenates byte sequences by copying them.

Comparison

boolean result = bytes1.equals(bytes2);
boolean result = Bytes.equals(bytes1, bytes2);
var result = cn.equalBytes(bytes1, bytes2);
my $result = $bytes1 eq $bytes2;

Returns true if the two bytes sequences are equal.

int result = bytes1.compareTo(bytes2);
var result = cn.compareBytes(bytes1, bytes2);
my $result = $bytes1 cmp $bytes2;

Compares the two byte sequences, and returns

-1 if bytes1 < bytes2
1 if bytes1 > bytes2
0 if bytes1 = bytes2

Accessing single bytes

		byte result = bytes.getInteger8(i);
		short result = bytes.getUnsigned8(i);
	
var result = bytes[i];
my $result = ord(substr($bytes, $i, 1));

Returns the byte value at position i.

Serialization

Bytes bytes = Bytes.fromBoolean(value);
Bytes bytes = Bytes.fromInteger(value);
Bytes bytes = Bytes.fromUnsigned(value);
Bytes bytes = Bytes.fromText(text);

boolean value = bytes.asBoolean();
long value = bytes.asInteger();
long value = bytes.asUnsigned();
String text = bytes.asText();
var bytes = cn.bytesFromBoolean(value);
var bytes = cn.bytesFromInteger(value);
var bytes = cn.bytesFromUnsigned(value);
var bytes = cn.bytesFromText(text);

var value = cn.booleanFromBytes(bytes);
var value = cn.integerFromBytes(bytes);
var value = cn.unsignedFromBytes(bytes);
var text = cn.textFromBytes(bytes);
my $bytes = CN->bytesFromBoolean($value);
my $bytes = CN->bytesFromInteger($value);
my $bytes = CN->bytesFromUnsigned($value);
my $bytes = Encode::encode_utf8($text);

my $value = CN->booleanFromBytes($bytes);
my $value = CN->integerFromBytes($bytes);
my $value = CN->unsignedFromBytes($bytes);
my $text = Encode::decode_utf8($bytes);

Serializes or deserializes values of common data types. These functions are rarely used directly, but often indirectly when constructing or querying records.

Boolean values are serialized to an empty byte sequence (false) or a single byte 121 (true).

Unsigned integers are serialized using big-endian (network) byte order, with as many bytes as necessary.

Signed integers are serialized similarly, but the first bit (most significant bit of the most significant byte) denotes the sign.

Text is serialized as a UTF-8 encoded sequence of Unicode characters.

There are no functions to serialize floating-point numbers.

Hex representation

Bytes bytes = Bytes.fromHex(hex);
String hex = bytes.asHex();
var bytes = cn.bytesFromHex(hex);
var hex = cn.hexFromBytes(bytes);
my $bytes = pack('H*', $hex);
my $hex = unpack('H*', $bytes);

Converts from and to a hex encoded string.