Light
Dark
System
v4latest
v4latest
v3
v2
v1

Bytes Functions and Operators

bytes

Byte sequence

bytes[i]

Accesses a byte at a given index.

bytes[from:to]

Produces a bytes sub-sequence from an existing bytes value.

bytes ++ bytes

Concatenates two bytes values into one.

= != ?= ?!= < > <= >=

Comparison operators

len()

Returns the number of bytes.

contains()

Checks if the byte sequence contains a given subsequence.

find()

Finds the index of the first occurrence of a subsequence.

to_bytes()

Converts a str value to bytes using UTF-8 encoding.

to_str()

Returns the string representation of the input value.

bytes_get_bit()

Returns the specified bit of the bytes value.

enc::base64_encode()

Returns a Base64-encoded str of the bytes value.

enc::base64_decode()

Returns the bytes of a Base64-encoded str.

type

bytes
bytes

A sequence of bytes representing raw data.

Bytes can be represented as a literal using this syntax: b''.

Copy
db> 
select b'Hello, world';
{b'Hello, world'}
Copy
db> 
select b'Hello,\x20world\x01';
{b'Hello, world\x01'}

There are also some generic functions that can operate on bytes:

Copy
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.

Copy
db> 
select <json>b'Hello EdgeDB!';
{"\"SGVsbG8gRWRnZURCIQ==\""}
Copy
db> 
select <bytes>to_json("\"SGVsbG8gRWRnZURCIQ==\"");
{b'Hello EdgeDB!'}

operator

bytes[i]
bytes [ int64 ] -> bytes

Accesses a byte at a given index.

Examples:

Copy
db> 
select b'binary \x01\x02\x03\x04 ftw!'[2];
{b'n'}
Copy
db> 
select b'binary \x01\x02\x03\x04 ftw!'[8];
{b'\x02'}

operator

bytes[from:to]
bytes [ int64 : int64 ] -> bytes

Produces a bytes sub-sequence from an existing bytes value.

Examples:

Copy
db> 
select b'\x01\x02\x03\x04 ftw!'[2:-1];
{b'\x03\x04 ftw'}
Copy
db> 
select b'some bytes'[2:-3];
{b'me by'}

operator

bytes ++ bytes
bytes ++ bytes -> bytes

Concatenates two bytes values into one.

Copy
db> 
select b'\x01\x02' ++ b'\x03\x04';
{b'\x01\x02\x03\x04'}

functionNew

to_bytes()
New
std::to_bytes(s: str) -> bytes

Converts a str value to bytes using UTF-8 encoding.

Copy
db> 
select to_bytes('テキスト');
{b'\xe3\x83\x86\xe3\x82\xad\xe3\x82\xb9\xe3\x83\x88'}

function

bytes_get_bit()
std::bytes_get_bit(bytes: bytes, nth: int64) -> int64

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.

Copy
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}

functionNew

enc::base64_encode()
New
enc::base64_encode(b: bytes) -> str

Returns a Base64-encoded str of the bytes value.

Copy
db> 
select enc::base64_encode(b'hello');
{'aGVsbG8='}

functionNew

enc::base64_decode()
New
enc::base64_decode(s: str) -> bytes

Returns the bytes of a Base64-encoded str.

Returns an InvalidValueError if input is not valid Base64.

Copy
db> 
select enc::base64_decode('aGVsbG8=');
{b'hello'}
Copy
db> 
select enc::base64_decode('aGVsbG8');
edgedb error: InvalidValueError: invalid base64 end sequence
Light
Dark
System

We use ChatGPT with additional context from our documentation to answer your questions. Not all answers will be accurate. Please join our Discord if you need more help.