> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8a08bda2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> Documentation for encoding functions

# Encoding functions

{/*AUTOGENERATED_START*/}

<h2 id="bech32Decode">
  bech32Decode
</h2>

Introduced in: v25.6.0

Decodes a Bech32 address string generated by either the bech32 or bech32m algorithms.

<Note>
  Unlike the encode function, `bech32Decode` will automatically handle padded FixedStrings.
</Note>

**Syntax**

```sql theme={null}
bech32Decode(address[, 'raw'])
```

**Arguments**

* `address` — A Bech32 string to decode. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)
* `mode` — Optional. Pass `'raw'` to decode without stripping the first byte as a witness version. Use this for non-SegWit addresses (e.g. Cosmos SDK). [`String`](/reference/data-types/string)

**Returned value**

Returns a tuple consisting of `(hrp, data)` that was used to encode the string. The data is in binary format. [`Tuple(String, String)`](/reference/data-types/tuple)

**Examples**

**Decode address**

```sql title=Query theme={null}
SELECT tup.1 AS hrp, hex(tup.2) AS data FROM (SELECT bech32Decode('bc1w508d6qejxtdg4y5r3zarvary0c5xw7kj7gz7z') AS tup)
```

```response title=Response theme={null}
bc   751E76E8199196D454941C45D1B3A323F1433BD6
```

**Testnet address**

```sql title=Query theme={null}
SELECT tup.1 AS hrp, hex(tup.2) AS data FROM (SELECT bech32Decode('tb1w508d6qejxtdg4y5r3zarvary0c5xw7kzp034v') AS tup)
```

```response title=Response theme={null}
tb   751E76E8199196D454941C45D1B3A323F1433BD6
```

<h2 id="bech32Encode">
  bech32Encode
</h2>

Introduced in: v25.6.0

Encodes a binary data string, along with a human-readable part (HRP), using the [Bech32 or Bech32m](https://en.bitcoin.it/wiki/Bech32) algorithms.

<Note>
  When using the [`FixedString`](/reference/data-types/fixedstring) data type, if a value does not fully fill the row it is padded with null characters.
  While the `bech32Encode` function will handle this automatically for the hrp argument, for the data argument the values must not be padded.
  For this reason it is not recommended to use the [`FixedString`](/reference/data-types/fixedstring) data type for your data values unless you are
  certain that they are all the same length and ensure that your `FixedString` column is set to that length as well.
</Note>

**Syntax**

```sql theme={null}
bech32Encode(hrp, data[, witver | 'bech32' | 'bech32m'])
```

**Arguments**

* `hrp` — A String of `1 - 83` lowercase characters specifying the "human-readable part" of the code. Usually 'bc' or 'tb'. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)
* `data` — A String of binary data to encode. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)
* `witver_or_variant` — Optional. Either a UInt\* witness version (default = 1, `0` for Bech32, `1`+ for Bech32m) or a String encoding variant: `'bech32'` (BIP173) or `'bech32m'` (BIP350). When a string variant is used, no witness version byte is prepended — this is needed for non-SegWit addresses such as Cosmos SDK. [`UInt*`](/reference/data-types/int-uint) or [`String`](/reference/data-types/string)

**Returned value**

Returns a Bech32 address string, consisting of the human-readable part, a separator character which is always '1', and a data part. The length of the string will never exceed 90 characters. If the algorithm cannot generate a valid address from the input, it will return an empty string. [`String`](/reference/data-types/string)

**Examples**

**Default Bech32m**

```sql title=Query theme={null}
-- When no witness version is supplied, the default is 1, the updated Bech32m algorithm.
SELECT bech32Encode('bc', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'))
```

```response title=Response theme={null}
bc1w508d6qejxtdg4y5r3zarvary0c5xw7k8zcwmq
```

**Bech32 algorithm**

```sql title=Query theme={null}
-- A witness version of 0 will result in a different address string.
SELECT bech32Encode('bc', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'), 0)
```

```response title=Response theme={null}
bc1w508d6qejxtdg4y5r3zarvary0c5xw7kj7gz7z
```

**Custom HRP**

```sql title=Query theme={null}
-- While 'bc' (Mainnet) and 'tb' (Testnet) are the only allowed hrp values for the
-- SegWit address format, Bech32 allows any hrp that satisfies the above requirements.
SELECT bech32Encode('abcdefg', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'), 10)
```

```response title=Response theme={null}
abcdefg1w508d6qejxtdg4y5r3zarvary0c5xw7k9rp8r4
```

**Cosmos SDK address (BIP173, no witness version)**

```sql title=Query theme={null}
-- Using 'bech32' variant encodes raw data without a witness version byte,
-- compatible with Cosmos SDK, Injective, Osmosis, and other non-SegWit chains.
SELECT bech32Encode('inj', unhex('751e76e8199196d454941c45d1b3a323f1433bd6'), 'bech32')
```

```response title=Response theme={null}
inj1w508d6qejxtdg4y5r3zarvary0c5xw7kgj5aqs
```

<h2 id="bin">
  bin
</h2>

Introduced in: v21.8.0

Returns a string containing the argument's binary representation according
to the following logic for different types:

| Type                       | Description                                                                                                                                                                                                                                                            |
| -------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `(U)Int*`                  | Prints bin digits from the most significant to least significant (big-endian or "human-readable" order). It starts with the most significant non-zero byte (leading zero bytes are omitted) but always prints eight digits of every byte if the leading digit is zero. |
| `Date` and `DateTime`      | Formatted as corresponding integers (the number of days since epoch for Date and the value of unix timestamp for DateTime).                                                                                                                                            |
| `String` and `FixedString` | All bytes are simply encoded as eight binary numbers. Zero bytes are not omitted.                                                                                                                                                                                      |
| `Float*` and `Decimal`     | Encoded as their representation in memory. As we support little-endian architecture, they are encoded in little-endian. Zero leading/trailing bytes are not omitted.                                                                                                   |
| `UUID`                     | Encoded as big-endian order string.                                                                                                                                                                                                                                    |

**Syntax**

```sql theme={null}
bin(arg)
```

**Arguments**

* `arg` — A value to convert to binary. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring) or [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime)

**Returned value**

Returns a string with the binary representation of the argument. [`String`](/reference/data-types/string)

**Examples**

**Simple integer**

```sql title=Query theme={null}
SELECT bin(14)
```

```response title=Response theme={null}
┌─bin(14)──┐
│ 00001110 │
└──────────┘
```

**Float32 numbers**

```sql title=Query theme={null}
SELECT bin(toFloat32(number)) AS bin_presentation FROM numbers(15, 2)
```

```response title=Response theme={null}
┌─bin_presentation─────────────────┐
│ 00000000000000000111000001000001 │
│ 00000000000000001000000001000001 │
└──────────────────────────────────┘
```

**Float64 numbers**

```sql title=Query theme={null}
SELECT bin(toFloat64(number)) AS bin_presentation FROM numbers(15, 2)
```

```response title=Response theme={null}
┌─bin_presentation─────────────────────────────────────────────────┐
│ 0000000000000000000000000000000000000000000000000010111001000000 │
│ 0000000000000000000000000000000000000000000000000011000001000000 │
└──────────────────────────────────────────────────────────────────┘
```

**UUID conversion**

```sql title=Query theme={null}
SELECT bin(toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0')) AS bin_uuid
```

```response title=Response theme={null}
┌─bin_uuid─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ 01100001111100001100010000000100010111001011001100010001111001111001000001111011101001100000000001101010110100111101101110100000 │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
```

<h2 id="bitPositionsToArray">
  bitPositionsToArray
</h2>

Introduced in: v21.7.0

This function returns the positions (in ascending order) of the 1 bits in the binary representation of an unsigned integer.
Signed input integers are first casted to an unsigned integer.

**Syntax**

```sql theme={null}
bitPositionsToArray(arg)
```

**Arguments**

* `arg` — An integer value. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns an array with the ascendingly ordered positions of 1 bits in the binary representation of the input. [`Array(UInt64)`](/reference/data-types/array)

**Examples**

**Single bit set**

```sql title=Query theme={null}
SELECT bitPositionsToArray(toInt8(1)) AS bit_positions
```

```response title=Response theme={null}
┌─bit_positions─┐
│ [0]           │
└───────────────┘
```

**All bits set**

```sql title=Query theme={null}
SELECT bitPositionsToArray(toInt8(-1)) AS bit_positions
```

```response title=Response theme={null}
┌─bit_positions─────────────┐
│ [0, 1, 2, 3, 4, 5, 6, 7]  │
└───────────────────────────┘
```

<h2 id="bitmaskToArray">
  bitmaskToArray
</h2>

Introduced in: v1.1.0

This function decomposes an integer into a sum of powers of two.
The powers of two are returned as an ascendingly ordered array.

**Syntax**

```sql theme={null}
bitmaskToArray(num)
```

**Arguments**

* `num` — An integer value. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns an array with the ascendingly ordered powers of two which sum up to the input number. [`Array(UInt64)`](/reference/data-types/array)

**Examples**

**Basic example**

```sql title=Query theme={null}
SELECT bitmaskToArray(50) AS powers_of_two
```

```response title=Response theme={null}
┌─powers_of_two───┐
│ [2, 16, 32]     │
└─────────────────┘
```

**Single power of two**

```sql title=Query theme={null}
SELECT bitmaskToArray(8) AS powers_of_two
```

```response title=Response theme={null}
┌─powers_of_two─┐
│ [8]           │
└───────────────┘
```

<h2 id="bitmaskToList">
  bitmaskToList
</h2>

Introduced in: v1.1.0

Like bitmaskToArray but returns the powers of two as a comma-separated string.

**Syntax**

```sql theme={null}
bitmaskToList(num)
```

**Arguments**

* `num` — An integer value. [`(U)Int*`](/reference/data-types/int-uint)

**Returned value**

Returns a string containing comma-separated powers of two. [`String`](/reference/data-types/string)

**Examples**

**Basic example**

```sql title=Query theme={null}
SELECT bitmaskToList(50) AS powers_list
```

```response title=Response theme={null}
┌─powers_list───┐
│ 2, 16, 32     │
└───────────────┘
```

<h2 id="char">
  char
</h2>

Introduced in: v20.1.0

Returns a string with length equal to the number of arguments passed where each byte
has the value of the corresponding argument. Accepts multiple arguments of numeric types.

If the value of the argument is out of range of the `UInt8` data type, then it is converted
to `UInt8` with potential rounding and overflow.

**Syntax**

```sql theme={null}
char(num1[, num2[, ...]])
```

**Arguments**

* `num1[, num2[, num3 ...]]` — Numerical arguments interpreted as integers. [`(U)Int8/16/32/64`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float)

**Returned value**

Returns a string of the given bytes. [`String`](/reference/data-types/string)

**Examples**

**Basic example**

```sql title=Query theme={null}
SELECT char(104.1, 101, 108.9, 108.9, 111) AS hello;
```

```response title=Response theme={null}
┌─hello─┐
│ hello │
└───────┘
```

**Constructing arbitrary encodings**

```sql title=Query theme={null}
-- You can construct a string of arbitrary encoding by passing the corresponding bytes.
-- for example UTF8
SELECT char(0xD0, 0xBF, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5, 0xD1, 0x82) AS hello;
```

```response title=Response theme={null}
┌─hello──┐
│ привет │
└────────┘
```

<h2 id="hex">
  hex
</h2>

Introduced in: v1.1.0

Returns a string containing the argument's hexadecimal representation according
to the following logic for different types:

| Type                       | Description                                                                                                                                                                                                                                                                            |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `(U)Int*`                  | Prints hex digits ("nibbles") from the most significant to least significant (big-endian or "human-readable" order). It starts with the most significant non-zero byte (leading zero bytes are omitted) but always prints both digits of every byte even if the leading digit is zero. |
| `Date` and `DateTime`      | Formatted as corresponding integers (the number of days since epoch for Date and the value of unix timestamp for DateTime).                                                                                                                                                            |
| `String` and `FixedString` | All bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.                                                                                                                                                                                                   |
| `Float*` and `Decimal`     | Encoded as their representation in memory. ClickHouse represents the values internally always as little endian, therefore they are encoded as such. Zero leading/trailing bytes are not omitted.                                                                                       |
| `UUID`                     | Encoded as big-endian order string.                                                                                                                                                                                                                                                    |

The function uses uppercase letters `A-F` and not using any prefixes (like `0x`) or suffixes (like `h`).

**Syntax**

```sql theme={null}
hex(arg)
```

**Arguments**

* `arg` — A value to convert to hexadecimal. [`String`](/reference/data-types/string) or [`(U)Int*`](/reference/data-types/int-uint) or [`Float*`](/reference/data-types/float) or [`Decimal`](/reference/data-types/decimal) or [`Date`](/reference/data-types/date) or [`DateTime`](/reference/data-types/datetime)

**Returned value**

Returns a string with the hexadecimal representation of the argument. [`String`](/reference/data-types/string)

**Examples**

**Simple integer**

```sql title=Query theme={null}
SELECT hex(1)
```

```response title=Response theme={null}
01
```

**Float32 numbers**

```sql title=Query theme={null}
SELECT hex(toFloat32(number)) AS hex_presentation FROM numbers(15, 2)
```

```response title=Response theme={null}
┌─hex_presentation─┐
│ 00007041         │
│ 00008041         │
└──────────────────┘
```

**Float64 numbers**

```sql title=Query theme={null}
SELECT hex(toFloat64(number)) AS hex_presentation FROM numbers(15, 2)
```

```response title=Response theme={null}
┌─hex_presentation─┐
│ 0000000000002E40 │
│ 0000000000003040 │
└──────────────────┘
```

**UUID conversion**

```sql title=Query theme={null}
SELECT lower(hex(toUUID('61f0c404-5cb3-11e7-907b-a6006ad3dba0'))) AS uuid_hex
```

```response title=Response theme={null}
┌─uuid_hex─────────────────────────┐
│ 61f0c4045cb311e7907ba6006ad3dba0 │
└──────────────────────────────────┘
```

<h2 id="hilbertDecode">
  hilbertDecode
</h2>

Introduced in: v24.6.0

Decodes a Hilbert curve index back into a tuple of unsigned integers, representing coordinates in multi-dimensional space.

As with the `hilbertEncode` function, this function has two modes of operation:

* **Simple**
* **Expanded**

**Simple mode**

Accepts up to 2 unsigned integers as arguments and produces a `UInt64` code.

**Expanded mode**

Accepts a range mask (tuple) as a first argument and up to 2 unsigned integers as
other arguments. Each number in the mask configures the number of bits by which
the corresponding argument will be shifted left, effectively scaling the argument
within its range.

Range expansion can be beneficial when you need a similar distribution for
arguments with wildly different ranges (or cardinality) For example: 'IP Address' `(0...FFFFFFFF)`
and 'Country code' `(0...FF)`. As with the encode function, this is limited to 8
numbers at most.

**Syntax**

```sql theme={null}
hilbertDecode(tuple_size, code)
```

**Arguments**

* `tuple_size` — Integer value of no more than `2`. [`UInt8/16/32/64`](/reference/data-types/int-uint) or [`Tuple(UInt8/16/32/64)`](/reference/data-types/tuple)
* `code` — `UInt64` code. [`UInt64`](/reference/data-types/int-uint)

**Returned value**

Returns a tuple of the specified size. [`Tuple(UInt64)`](/reference/data-types/tuple)

**Examples**

**Simple mode**

```sql title=Query theme={null}
SELECT hilbertDecode(2, 31)
```

```response title=Response theme={null}
["3", "4"]
```

**Single argument**

```sql title=Query theme={null}
-- Hilbert code for one argument is always the argument itself (as a tuple).
SELECT hilbertDecode(1, 1)
```

```response title=Response theme={null}
["1"]
```

**Expanded mode**

```sql title=Query theme={null}
-- A single argument with a tuple specifying bit shifts will be right-shifted accordingly.
SELECT hilbertDecode(tuple(2), 32768)
```

```response title=Response theme={null}
["128"]
```

**Column usage**

```sql title=Query theme={null}
-- First create the table and insert some data
CREATE TABLE hilbert_numbers(
    n1 UInt32,
    n2 UInt32
)
ENGINE=MergeTree()
ORDER BY n1 SETTINGS index_granularity_bytes = '10Mi';
insert into hilbert_numbers (*) values(1,2);

-- Use column names instead of constants as function arguments
SELECT untuple(hilbertDecode(2, hilbertEncode(n1, n2))) FROM hilbert_numbers;
```

```response title=Response theme={null}
1    2
```

<h2 id="hilbertEncode">
  hilbertEncode
</h2>

Introduced in: v24.6.0

Calculates code for Hilbert Curve for a list of unsigned integers.

The function has two modes of operation:

* **Simple**
* **Expanded**

**Simple mode**

Accepts up to 2 unsigned integers as arguments and produces a UInt64 code.

**Expanded mode**

Accepts a range mask ([Tuple](/reference/data-types/tuple)) as the
first argument and up to 2 [unsigned integers](/reference/data-types/int-uint)
as other arguments.

Each number in the mask configures the number of bits by which the corresponding
argument will be shifted left, effectively scaling the argument within its range.

**Syntax**

```sql theme={null}
-- Simplified mode
hilbertEncode(args)

-- Expanded mode
hilbertEncode(range_mask, args)
```

**Arguments**

* `args` — Up to two `UInt` values or columns of type `UInt`. [`UInt8/16/32/64`](/reference/data-types/int-uint)
* `range_mask` — For the expanded mode, up to two `UInt` values or columns of type `UInt`. [`UInt8/16/32/64`](/reference/data-types/int-uint)

**Returned value**

Returns a `UInt64` code. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Simple mode**

```sql title=Query theme={null}
SELECT hilbertEncode(3, 4)
```

```response title=Response theme={null}
31
```

**Expanded mode**

```sql title=Query theme={null}
-- Range expansion can be beneficial when you need a similar distribution for
-- arguments with wildly different ranges (or cardinality).
-- For example: 'IP Address' (0...FFFFFFFF) and 'Country code' (0...FF).
-- Note: tuple size must be equal to the number of the other arguments.
SELECT hilbertEncode((10, 6), 1024, 16)
```

```response title=Response theme={null}
4031541586602
```

**Single argument**

```sql title=Query theme={null}
-- For a single argument without a tuple, the function returns the argument
-- itself as the Hilbert index, since no dimensional mapping is needed.
SELECT hilbertEncode(1)
```

```response title=Response theme={null}
1
```

**Expanded single argument**

```sql title=Query theme={null}
-- If a single argument is provided with a tuple specifying bit shifts, the function
-- shifts the argument left by the specified number of bits.
SELECT hilbertEncode(tuple(2), 128)
```

```response title=Response theme={null}
512
```

**Column usage**

```sql title=Query theme={null}
-- First create the table and insert some data
CREATE TABLE hilbert_numbers(
    n1 UInt32,
    n2 UInt32
)
ENGINE=MergeTree()
ORDER BY n1;
insert into hilbert_numbers (*) values(1, 2);

-- Use column names instead of constants as function arguments
SELECT hilbertEncode(n1, n2) FROM hilbert_numbers;
```

```response title=Response theme={null}
13
```

<h2 id="mortonDecode">
  mortonDecode
</h2>

Introduced in: v24.6.0

Decodes a Morton encoding (ZCurve) into the corresponding unsigned integer tuple.

As with the `mortonEncode` function, this function has two modes of operation:

* **Simple**
* **Expanded**

**Simple mode**

Accepts a resulting tuple size as the first argument and the code as the second argument.

**Expanded mode**

Accepts a range mask (tuple) as the first argument and the code as the second argument.
Each number in the mask configures the amount of range shrink:

* `1` - no shrink
* `2` - 2x shrink
* `3` - 3x shrink
  ⋮
* Up to 8x shrink.

Range expansion can be beneficial when you need a similar distribution for
arguments with wildly different ranges (or cardinality). For example: 'IP Address' `(0...FFFFFFFF)`
and 'Country code' `(0...FF)`. As with the encode function, this is limited to
8 numbers at most.

**Syntax**

```sql theme={null}
-- Simple mode
mortonDecode(tuple_size, code)

-- Expanded mode
mortonDecode(range_mask, code)
```

**Arguments**

* `tuple_size` — Integer value no more than 8. [`UInt8/16/32/64`](/reference/data-types/int-uint)
* `range_mask` — For the expanded mode, the mask for each argument. The mask is a tuple of unsigned integers. Each number in the mask configures the amount of range shrink. [`Tuple(UInt8/16/32/64)`](/reference/data-types/tuple)
* `code` — UInt64 code. [`UInt64`](/reference/data-types/int-uint)

**Returned value**

Returns a tuple of the specified size. [`Tuple(UInt64)`](/reference/data-types/tuple)

**Examples**

**Simple mode**

```sql title=Query theme={null}
SELECT mortonDecode(3, 53)
```

```response title=Response theme={null}
["1", "2", "3"]
```

**Single argument**

```sql title=Query theme={null}
SELECT mortonDecode(1, 1)
```

```response title=Response theme={null}
["1"]
```

**Expanded mode, shrinking one argument**

```sql title=Query theme={null}
SELECT mortonDecode(tuple(2), 32768)
```

```response title=Response theme={null}
["128"]
```

**Column usage**

```sql title=Query theme={null}
-- First create the table and insert some data
CREATE TABLE morton_numbers(
    n1 UInt32,
    n2 UInt32,
    n3 UInt16,
    n4 UInt16,
    n5 UInt8,
    n6 UInt8,
    n7 UInt8,
    n8 UInt8
)
ENGINE=MergeTree()
ORDER BY n1;
INSERT INTO morton_numbers (*) values(1, 2, 3, 4, 5, 6, 7, 8);

-- Use column names instead of constants as function arguments
SELECT untuple(mortonDecode(8, mortonEncode(n1, n2, n3, n4, n5, n6, n7, n8))) FROM morton_numbers;
```

```response title=Response theme={null}
1 2 3 4 5 6 7 8
```

<h2 id="mortonEncode">
  mortonEncode
</h2>

Introduced in: v24.6.0

Calculates the Morton encoding (ZCurve) for a list of unsigned integers.

The function has two modes of operation:

* **Simple**
* *Expanded*\*

**Simple mode**

Accepts up to 8 unsigned integers as arguments and produces a `UInt64` code.

**Expanded mode**

Accepts a range mask ([Tuple](/reference/data-types/tuple)) as the first argument and
up to 8 [unsigned integers](/reference/data-types/int-uint) as other arguments.

Each number in the mask configures the amount of range expansion:

* 1 - no expansion
* 2 - 2x expansion
* 3 - 3x expansion
  ⋮
* Up to 8x expansion.

**Syntax**

```sql theme={null}
-- Simplified mode
mortonEncode(args)

-- Expanded mode
mortonEncode(range_mask, args)
```

**Arguments**

* `args` — Up to 8 unsigned integers or columns of the aforementioned type. [`UInt8/16/32/64`](/reference/data-types/int-uint)
* `range_mask` — For the expanded mode, the mask for each argument. The mask is a tuple of unsigned integers from `1` - `8`. Each number in the mask configures the amount of range shrink. [`Tuple(UInt8/16/32/64)`](/reference/data-types/tuple)

**Returned value**

Returns a `UInt64` code. [`UInt64`](/reference/data-types/int-uint)

**Examples**

**Simple mode**

```sql title=Query theme={null}
SELECT mortonEncode(1, 2, 3)
```

```response title=Response theme={null}
53
```

**Expanded mode**

```sql title=Query theme={null}
-- Range expansion can be beneficial when you need a similar distribution for
-- arguments with wildly different ranges (or cardinality)
-- For example: 'IP Address' (0...FFFFFFFF) and 'Country code' (0...FF).
-- Note: the Tuple size must be equal to the number of the other arguments.
SELECT mortonEncode((1,2), 1024, 16)
```

```response title=Response theme={null}
1572864
```

**Single argument**

```sql title=Query theme={null}
-- Morton encoding for one argument is always the argument itself
SELECT mortonEncode(1)
```

```response title=Response theme={null}
1
```

**Expanded single argument**

```sql title=Query theme={null}
SELECT mortonEncode(tuple(2), 128)
```

```response title=Response theme={null}
32768
```

**Column usage**

```sql title=Query theme={null}
-- First create the table and insert some data
CREATE TABLE morton_numbers(
    n1 UInt32,
    n2 UInt32,
    n3 UInt16,
    n4 UInt16,
    n5 UInt8,
    n6 UInt8,
    n7 UInt8,
    n8 UInt8
)
ENGINE=MergeTree()
ORDER BY n1;
INSERT INTO morton_numbers (*) values(1, 2, 3, 4, 5, 6, 7, 8);

-- Use column names instead of constants as function arguments
SELECT mortonEncode(n1, n2, n3, n4, n5, n6, n7, n8) FROM morton_numbers;
```

```response title=Response theme={null}
2155374165
```

<h2 id="sqidDecode">
  sqidDecode
</h2>

Introduced in: v24.1.0

Transforms a [sqid](https://sqids.org/) back into an array of numbers.

**Syntax**

```sql theme={null}
sqidDecode(sqid)
```

**Arguments**

* `sqid` — The sqid to decode. [`String`](/reference/data-types/string)

**Returned value**

Returns an array of numbers from `sqid`. [`Array(UInt64)`](/reference/data-types/array)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT sqidDecode('gXHfJ1C6dN');
```

```response title=Response theme={null}
┌─sqidDecode('gXHfJ1C6dN')─────┐
│ [1, 2, 3, 4, 5]              │
└──────────────────────────────┘
```

<h2 id="sqidEncode">
  sqidEncode
</h2>

Introduced in: v24.1.0

Transforms numbers into a [sqid](https://sqids.org/), a Youtube-like ID string.

**Syntax**

```sql theme={null}
sqidEncode(n1[, n2, ...])
```

**Aliases**: `sqid`

**Arguments**

* `n1[, n2, ...]` — Arbitrarily many numbers. [`UInt8/16/32/64`](/reference/data-types/int-uint)

**Returned value**

Returns a hash ID [`String`](/reference/data-types/string)

**Examples**

**Usage example**

```sql title=Query theme={null}
SELECT sqidEncode(1, 2, 3, 4, 5);
```

```response title=Response theme={null}
┌─sqidEncode(1, 2, 3, 4, 5)─┐
│ gXHfJ1C6dN                │
└───────────────────────────┘
```

<h2 id="unbin">
  unbin
</h2>

Introduced in: v21.8.0

Interprets each pair of binary digits (in the argument) as a number and converts it to the byte represented by the number. The functions performs the opposite operation to bin.

For a numeric argument `unbin()` does not return the inverse of `bin()`. If you want to convert the result to a number, you can use the reverse and `reinterpretAs<Type>` functions.

<Note>
  If `unbin` is invoked from within the `clickhouse-client`, binary strings are displayed using UTF-8.
</Note>

Supports binary digits `0` and `1`. The number of binary digits does not have to be multiples of eight. If the argument string contains anything other than binary digits,
the result is undefined (no exception is thrown).

**Syntax**

```sql theme={null}
unbin(arg)
```

**Arguments**

* `arg` — A string containing any number of binary digits. [`String`](/reference/data-types/string)

**Returned value**

Returns a binary string (BLOB). [`String`](/reference/data-types/string)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT UNBIN('001100000011000100110010'), UNBIN('0100110101111001010100110101000101001100')
```

```response title=Response theme={null}
┌─unbin('001100000011000100110010')─┬─unbin('0100110101111001010100110101000101001100')─┐
│ 012                               │ MySQL                                             │
└───────────────────────────────────┴───────────────────────────────────────────────────┘
```

**Convert to number**

```sql title=Query theme={null}
SELECT reinterpretAsUInt64(reverse(unbin('1110'))) AS num
```

```response title=Response theme={null}
┌─num─┐
│  14 │
└─────┘
```

<h2 id="unhex">
  unhex
</h2>

Introduced in: v1.1.0

Performs the opposite operation of [`hex`](#hex). It interprets each pair of hexadecimal digits (in the argument) as a number and converts
it to the byte represented by the number. The returned value is a binary string (BLOB).

If you want to convert the result to a number, you can use the `reverse` and `reinterpretAs<Type>` functions.

<Note>
  `clickhouse-client` interprets strings as UTF-8.
  This may cause that values returned by `hex` to be displayed surprisingly.
</Note>

Supports both uppercase and lowercase letters `A-F`.
The number of hexadecimal digits does not have to be even.
If it is odd, the last digit is interpreted as the least significant half of the `00-0F` byte.
If the argument string contains anything other than hexadecimal digits, some implementation-defined result is returned (an exception isn't thrown).
For a numeric argument the inverse of hex(N) is not performed by unhex().

**Syntax**

```sql theme={null}
unhex(arg)
```

**Arguments**

* `arg` — A string containing any number of hexadecimal digits. [`String`](/reference/data-types/string) or [`FixedString`](/reference/data-types/fixedstring)

**Returned value**

Returns a binary string (BLOB). [`String`](/reference/data-types/string)

**Examples**

**Basic usage**

```sql title=Query theme={null}
SELECT unhex('303132'), UNHEX('4D7953514C')
```

```response title=Response theme={null}
┌─unhex('303132')─┬─unhex('4D7953514C')─┐
│ 012             │ MySQL               │
└─────────────────┴─────────────────────┘
```

**Convert to number**

```sql title=Query theme={null}
SELECT reinterpretAsUInt64(reverse(unhex('FFF'))) AS num
```

```response title=Response theme={null}
┌──num─┐
│ 4095 │
└──────┘
```
