Bytes Functions and Operators
Byte sequence | |
An enum for indicating integer value encoding. | |
Accesses a byte at a given index. | |
Produces a bytes sub-sequence from an existing bytes value. | |
Concatenates two bytes values into one. | |
Comparison operators | |
Returns the number of bytes. | |
Checks if the byte sequence contains a given subsequence. | |
Finds the index of the first occurrence of a subsequence. | |
Converts a given value into binary representation as bytes. | |
Returns the string representation of the input value. | |
Returns an int16 value parsed from the given input. | |
Returns an int32 value parsed from the given input. | |
Returns an int64 value parsed from the given input. | |
Returns a uuid value parsed from 128-bit input. | |
Returns the specified bit of the bytes value. | |
Return the number of bits set in the bytes value. | |
Returns a Base64-encoded str of the bytes value. | |
Returns the bytes of a Base64-encoded str. |
A sequence of bytes representing raw data.
Bytes can be represented as a literal using this syntax: b''
.
db>
select b'Hello, world';
{b'Hello, world'}
db>
select b'Hello,\x20world\x01';
{b'Hello, world\x01'}
There are also some generic functions that can operate on bytes:
db>
select contains(b'qwerty', b'42');
{false}
Bytes are rendered as base64-encoded strings in JSON. When you cast a
bytes
value into JSON, that’s what you’ll get. In order to
cast
a json
value into bytes, it must be a
base64-encoded string.
db>
select <json>b'Hello EdgeDB!';
{"\"SGVsbG8gRWRnZURCIQ==\""}
db>
select <bytes>to_json("\"SGVsbG8gRWRnZURCIQ==\"");
{b'Hello EdgeDB!'}
An enum for indicating integer value encoding.
This enum is used by the to_int16()
, to_int32()
,
to_int64()
and the to_bytes()
converters working with
bytes
and integers.
Endian.Big
stands for big-endian encoding going from most significant
byte to least. Endian.Little
stands for little-endian encoding going
from least to most significant byte.
db>
select to_bytes(<int32>16908295, Endian.Big);
{b'\x01\x02\x00\x07'}
db>
select to_int32(b'\x01\x02\x00\x07', Endian.Big);
{16908295}
db>
select to_bytes(<int32>16908295, Endian.Little);
{b'\x07\x00\x02\x01'}
db>
select to_int32(b'\x07\x00\x02\x01', Endian.Little);
{16908295}
Converts a given value into binary representation as bytes
.
The strings get converted using UTF-8 encoding:
db>
select to_bytes('テキスト');
{b'\xe3\x83\x86\xe3\x82\xad\xe3\x82\xb9\xe3\x83\x88'}
The integer values can be encoded as big-endian (most significant bit comes first) byte strings:
db>
select to_bytes(<int16>31, Endian.Big);
{b'\x00\x1f'}
db>
select to_bytes(<int32>31, Endian.Big);
{b'\x00\x00\x00\x1f'}
db>
select to_bytes(123456789123456789, Endian.Big);
{b'\x01\xb6\x9bK\xac\xd0_\x15'}
Due to underlying implementation details using big-endian encoding
results in slightly faster performance of to_bytes
when converting
integers.
The UUID values are converted to the underlying string of 16 bytes:
db>
select to_bytes(<uuid>'1d70c86e-cc92-11ee-b4c7-a7aa0a34e2ae');
{b'\x1dp\xc8n\xcc\x92\x11\xee\xb4\xc7\xa7\xaa\n4\xe2\xae'}
To perform the reverse conversion there are corresponding functions:
to_str()
, to_int16()
, to_int32()
,
to_int64()
, to_uuid()
.
Returns the specified bit of the bytes
value.
When looking for the nth bit, this function will enumerate bits from least to most significant in each byte.
db> ... ...
for n in {0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13 ,14, 15}
union bytes_get_bit(b'ab', n);
{1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 0}