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 = cds.randomBytes(16);
my $randomBytes = CDS->randomBytes(16);
Creates a sequence of 16 random bytes.
Bytes randomBytes = Condensation.randomKey();
var randomBytes = cds.randomKey();
my $randomBytes = CDS->randomKey;
Creates a random AES key, i.e. a sequence of 32 random bytes.
Slicing
Bytes slice = bytes.slice(10, 4);
var slice = cds.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 = cds.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 = cds.equalBytes(bytes1, bytes2);
my $result = $bytes1 eq $bytes2;
Returns true if the two bytes sequences are equal.
int result = bytes1.compareTo(bytes2);
var result = cds.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 = cds.bytesFromBoolean(value); var bytes = cds.bytesFromInteger(value); var bytes = cds.bytesFromUnsigned(value); var bytes = cds.bytesFromText(text); var value = cds.booleanFromBytes(bytes); var value = cds.integerFromBytes(bytes); var value = cds.unsignedFromBytes(bytes); var text = cds.textFromBytes(bytes);
my $bytes = CDS->bytesFromBoolean($value); my $bytes = CDS->bytesFromInteger($value); my $bytes = CDS->bytesFromUnsigned($value); my $bytes = Encode::encode_utf8($text); my $value = CDS->booleanFromBytes($bytes); my $value = CDS->integerFromBytes($bytes); my $value = CDS->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 = cds.bytesFromHex(hex); var hex = cds.hexFromBytes(bytes);
my $bytes = pack('H*', $hex); my $hex = unpack('H*', $bytes);
Converts from and to a hex encoded string.