Skip to main content
Skip to main content

Encoding functions

char

Returns the string with the length as the number of passed arguments and each byte has the value of corresponding argument. Accepts multiple arguments of numeric types. If the value of argument is out of range of UInt8 data type, it is converted to UInt8 with possible rounding and overflow.

Syntax

Arguments

  • number_1, number_2, ..., number_n — Numerical arguments interpreted as integers. Types: Int, Float.

Returned value

  • a string of given bytes. String.

Example

Query:

Result:

You can construct a string of arbitrary encoding by passing the corresponding bytes. Here is example for UTF-8:

Query:

Result:

Query:

Result:

hex

Returns a string containing the argument's hexadecimal representation.

Alias: HEX.

Syntax

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

For integer arguments, it 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.

Values of type Date and DateTime are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).

For String and FixedString, all bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.

Values of Float and Decimal types are 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.

Values of UUID type are encoded as big-endian order string.

Arguments

Returned value

  • A string with the hexadecimal representation of the argument. String.

Examples

Query:

Result:

Query:

Result:

Query:

Result:

Query:

Result:

unhex

Performs the opposite operation of 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 return 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

If unhex is invoked from within the clickhouse-client, binary strings display using UTF-8.

Alias: UNHEX.

Syntax

Arguments

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().

Returned value

  • A binary string (BLOB). String.

Example

Query:

Result:

Query:

Result:

bin

Returns a string containing the argument's binary representation.

Syntax

Alias: BIN.

For integer arguments, it 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.

Values of type Date and DateTime are formatted as corresponding integers (the number of days since Epoch for Date and the value of Unix Timestamp for DateTime).

For String and FixedString, all bytes are simply encoded as eight binary numbers. Zero bytes are not omitted.

Values of Float and Decimal types are 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.

Values of UUID type are encoded as big-endian order string.

Arguments

Returned value

  • A string with the binary representation of the argument. String.

Examples

Query:

Result:

Query:

Result:

Query:

Result:

Query:

Result:

unbin

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.

Syntax

Alias: UNBIN.

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.

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, some implementation-defined result is returned (an exception isn't thrown).

Arguments

  • arg — A string containing any number of binary digits. String.

Returned value

  • A binary string (BLOB). String.

Examples

Query:

Result:

Query:

Result:

bitmaskToList(num)

Accepts an integer. Returns a string containing the list of powers of two that total the source number when summed. They are comma-separated without spaces in text format, in ascending order.

bitmaskToArray(num)

Accepts an integer. Returns an array of UInt64 numbers containing the list of powers of two that total the source number when summed. Numbers in the array are in ascending order.

bitPositionsToArray(num)

Accepts an integer and converts it to an unsigned integer. Returns an array of UInt64 numbers containing the list of positions of bits of arg that equal 1, in ascending order.

Syntax

Arguments

Returned value

  • An array containing a list of positions of bits that equal 1, in ascending order. Array(UInt64).

Example

Query:

Result:

Query:

Result:

mortonEncode

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.

Syntax

Parameters

Returned value

Example

Query:

Result:

Expanded mode

Accepts a range mask (tuple) as a first argument and up to 8 unsigned integers 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

Parameters

  • range_mask: 1-8.
  • args: up to 8 unsigned integers or columns of the aforementioned type.

Note: when using columns for args the provided range_mask tuple should still be a constant.

Returned value

Example

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

Query:

Result:

Note: tuple size must be equal to the number of the other arguments.

Example

Morton encoding for one argument is always the argument itself:

Query:

Result:

Example

It is also possible to expand one argument too:

Query:

Result:

Example

You can also use column names in the function.

Query:

First create the table and insert some data.

Use column names instead of constants as function arguments to mortonEncode

Query:

Result:

implementation details

Please note that you can fit only so many bits of information into Morton code as UInt64 has. Two arguments will have a range of maximum 2^32 (64/2) each, three arguments a range of max 2^21 (64/3) each and so on. All overflow will be clamped to zero.

mortonDecode

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.

Syntax

Parameters

  • tuple_size: integer value no more than 8.
  • code: UInt64 code.

Returned value

Example

Query:

Result:

Expanded mode

Accepts a range mask (tuple) as a 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.

Example

Query:

Result:

Example

It is also possible to shrink one argument:

Query:

Result:

Example

You can also use column names in the function.

First create the table and insert some data.

Query:

Use column names instead of constants as function arguments to mortonDecode

Query:

Result:

hilbertEncode

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

The function has two modes of operation:

  • Simple
  • Expanded

Simple mode

Simple: accepts up to 2 unsigned integers as arguments and produces a UInt64 code.

Syntax

Parameters

Returned value

  • A UInt64 code

Type: UInt64

Example

Query:

Result:

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.

Syntax

Parameters

Note: when using columns for args the provided range_mask tuple should still be a constant.

Returned value

  • A UInt64 code

Type: UInt64

Example

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

Query:

Result:

Note: tuple size must be equal to the number of the other arguments.

Example

For a single argument without a tuple, the function returns the argument itself as the Hilbert index, since no dimensional mapping is needed.

Query:

Result:

Example

If a single argument is provided with a tuple specifying bit shifts, the function shifts the argument left by the specified number of bits.

Query:

Result:

Example

The function also accepts columns as arguments:

Query:

First create the table and insert some data.

Use column names instead of constants as function arguments to hilbertEncode

Query:

Result:

implementation details

Please note that you can fit only so many bits of information into Hilbert code as UInt64 has. Two arguments will have a range of maximum 2^32 (64/2) each. All overflow will be clamped to zero.

hilbertDecode

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.

Syntax

Parameters

  • tuple_size: integer value no more than 2.
  • code: UInt64 code.

Returned value

  • tuple of the specified size.

Type: UInt64

Example

Query:

Result:

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.

Example

Hilbert code for one argument is always the argument itself (as a tuple).

Query:

Result:

Example

A single argument with a tuple specifying bit shifts will be right-shifted accordingly.

Query:

Result:

Example

The function accepts a column of codes as a second argument:

First create the table and insert some data.

Query:

Use column names instead of constants as function arguments to hilbertDecode

Query:

Result:

bech32Encode

Encodes a binary data string, along with a human-readable part (HRP), using the Bech32 or Bech32m algorithms.

Syntax

Parameters

  • hrp — String of 1 - 83 lowercase characters specifying the "human-readable part" of the code. Usually 'bc' or 'tb'. String or FixedString.
  • data — String of binary data to encode. String or FixedString.
  • witver - Witness version. Optional, default = 1. An UInt* specifying the version of the algorithm to run. 0 for Bech32 and 1 or greater for Bech32m.
Note

When using the 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 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.

Returned value

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

Type: String.

Example

When no witness version is supplied, the default is 1, the updated Bech32m algorithm.

Query:

Result:

A witness version of 0 will result in a different address string.

Query:

Result:

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.

Query:

Result:

bech32Decode

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

Syntax

Parameters

Note

Unlike the encode function, Bech32Decode will automatically handle padded FixedStrings.

Returned value

  • A tuple consisting of the (hrp, data) that was used to encode the string. The data is in binary format.

Type: (String, String).

Example

Query:

Result:

Query:

Result:

base64Decode

Introduced in: v18.16

Accepts a string and decodes it from base64, according to RFC 4648 (https://datatracker.ietf.org/doc/html/rfc4648#section-4). Throws an exception in case of an error. Alias: FROM_BASE64.

Syntax

Arguments

  • encoded — A string column. If the string is not a valid Base64-encoded value, an exception is thrown. String

Returned value

A string containing the decoded value of the argument. String

Examples

Example

base64URLDecode

Introduced in: v24.6

Accepts a base64-encoded URL and decodes it from base64 with URL-specific modifications, according to RFC 4648 (https://datatracker.ietf.org/doc/html/rfc4648#section-5).

Syntax

Arguments

  • encodedURL — String column or constant. If the string is not a valid Base64-encoded value, an exception is thrown.

Returned value

A string containing the decoded value of the argument. String

Examples

Example

base64URLEncode

Introduced in: v24.6

Encodes an URL (String or FixedString) as base64 with URL-specific modifications, according to RFC 4648 (https://datatracker.ietf.org/doc/html/rfc4648#section-5).

Syntax

Arguments

  • url — String column or constant.

Returned value

A string containing the encoded value of the argument. String

Examples

Example

bech32Decode

Introduced in: v25.6

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

Note

Unlike the encode function, Bech32Decode will automatically handle padded FixedStrings.

Syntax

Arguments

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)

Examples

Decode address

Testnet address

bech32Encode

Introduced in: v25.6

Encodes a binary data string, along with a human-readable part (HRP), using the Bech32 or Bech32m algorithms.

Note

When using the 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 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.

Syntax

Arguments

  • hrp — A String of 1 - 83 lowercase characters specifying the "human-readable part" of the code. Usually 'bc' or 'tb'. String or FixedString
  • data — A String of binary data to encode. String or FixedString
  • witver — Optional. The witness version (default = 1). An UInt* specifying the version of the algorithm to run. 0 for Bech32 and 1 or greater for Bech32m. UInt*

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

Examples

Default Bech32m

Bech32 algorithm

Custom HRP

bin

Introduced in: v21.8

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

TypeDescription
(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 DateTimeFormatted as corresponding integers (the number of days since epoch for Date and the value of unix timestamp for DateTime).
String and FixedStringAll bytes are simply encoded as eight binary numbers. Zero bytes are not omitted.
Float* and DecimalEncoded as their representation in memory. As we support little-endian architecture, they are encoded in little-endian. Zero leading/trailing bytes are not omitted.
UUIDEncoded as big-endian order string.

Syntax

Arguments

Returned value

Returns a string with the binary representation of the argument. String

Examples

Simple integer

Float32 numbers

Float64 numbers

UUID conversion

bitPositionsToArray

Introduced in: v21.7

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

Arguments

  • arg — An integer value. (U)Int*

Returned value

Returns an array with the ascendingly ordered positions of 1 bits in the binary representation of the input. Array(UInt64)

Examples

Single bit set

All bits set

bitmaskToArray

Introduced in: v1.1

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

Syntax

Arguments

  • num — An integer value. (U)Int*

Returned value

Returns an array with the ascendingly ordered powers of two which sum up to the input number. Array(UInt64)

Examples

Basic example

Single power of two

bitmaskToList

Introduced in: v1.1

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

Syntax

Arguments

  • num — An integer value. (U)Int*

Returned value

Returns a string containing comma-separated powers of two. String

Examples

Basic example

char

Introduced in: v20.1

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

Arguments

Returned value

Returns a string of the given bytes. String

Examples

Basic example

Constructing arbitrary encodings

hex

Introduced in: v1.1

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

TypeDescription
(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 DateTimeFormatted as corresponding integers (the number of days since epoch for Date and the value of unix timestamp for DateTime).
String and FixedStringAll bytes are simply encoded as two hexadecimal numbers. Zero bytes are not omitted.
Float* and DecimalEncoded 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.
UUIDEncoded as big-endian order string.

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

Syntax

Arguments

Returned value

Returns a string with the hexadecimal representation of the argument. String

Examples

Simple integer

Float32 numbers

Float64 numbers

UUID conversion

hilbertDecode

Introduced in: v24.6

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

Arguments

Returned value

Returns a tuple of the specified size. Tuple(UInt64)

Examples

Simple mode

Single argument

Expanded mode

Column usage

hilbertEncode

Introduced in: v24.6

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) as the 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.

Syntax

Arguments

  • args — Up to two UInt values or columns of type UInt. UInt8/16/32/64
  • range_mask — For the expanded mode, up to two UInt values or columns of type UInt. UInt8/16/32/64

Returned value

Returns a UInt64 code. UInt64

Examples

Simple mode

Expanded mode

Single argument

Expanded single argument

Column usage

idnaDecode

Introduced in: v

Computes the Unicode representation of ASCII-encoded Internationalized Domain Name.

Syntax

Arguments

  • str — Input string

Returned value

Returns a unicode-encoded domain name. String

Examples

simple

idnaEncode

Introduced in: v

Computes an ASCII representation of an Internationalized Domain Name. Throws an exception in case of error.

Syntax

Arguments

  • str — Input string

Returned value

Returns an ASCII-encoded domain name String

Examples

simple

mortonDecode

Introduced in: v24.6

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

Arguments

  • tuple_size — Integer value no more than 8. UInt8/16/32/64
  • 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)
  • code — UInt64 code. UInt64

Returned value

Returns a tuple of the specified size. Tuple(UInt64)

Examples

Simple mode

Single argument

Expanded mode, shrinking one argument

Column usage

mortonEncode

Introduced in: v24.6

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) as the first argument and up to 8 unsigned integers 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

Arguments

  • args — Up to 8 unsigned integers or columns of the aforementioned type. UInt8/16/32/64
  • 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)

Returned value

Returns a UInt64 code. UInt64

Examples

Simple mode

Expanded mode

Single argument

Expanded single argument

Column usage

punycodeDecode

Introduced in: v

Computes a Punycode representation of a string. Throws an exception if the input is not valid Punycode.

Syntax

Arguments

  • str — A Punycode-encoded string String

Returned value

The plaintext representation String

Examples

simple

punycodeEncode

Introduced in: v

Computes a Punycode representation of a string.

Syntax

Arguments

Returned value

The punycode representation String

Examples

simple

sqidDecode

Introduced in: v

Transforms a Sqid back into an array of numbers.

Syntax

Arguments

  • sqid — A sqid

Returned value

An array of numbers Array(UInt64)

Examples

simple

sqidEncode

Introduced in: v

Transforms numbers into a Sqid which is a Youtube-like ID string.

Syntax

Arguments

Returned value

A hash id String

Examples

simple

tryBase64Decode

Introduced in: v18.16

Decodes a String or FixedString from base64, like base64Decode but returns an empty string in case of an error.

Syntax

Arguments

  • encoded — String column or constant. If the string is not a valid Base64-encoded value, returns an empty string. String

Returned value

Returns a string containing the decoded value of the argument. String

Examples

valid

invalid

tryBase64URLDecode

Introduced in: v24.6

Decodes an URL from base64, like base64URLDecode but returns an empty string in case of an error.

Syntax

Arguments

  • encodedURL — String column or constant. If the string is not a valid Base64-encoded value with URL-specific modifications, returns an empty string. String

Returned value

Returns a string containing the decoded value of the argument. String

Examples

valid

invalid

tryIdnaEncode

Introduced in: v

Computes a ASCII representation of an Internationalized Domain Name. Returns an empty string in case of error

Syntax

Arguments

  • str — Input string

Returned value

Returns an ASCII-encoded domain name String

Examples

simple

tryPunycodeDecode

Introduced in: v

Computes a Punycode representation of a string. Returns an empty string if the input is not valid Punycode.

Syntax

Arguments

  • str — A Punycode-encoded string

Returned value

The plaintext representation String

Examples

simple

unbin

Introduced in: v21.8

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.

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

Arguments

  • arg — A string containing any number of binary digits. String

Returned value

Returns a binary string (BLOB). String

Examples

Basic usage

Convert to number

unhex

Introduced in: v1.1

Performs the opposite operation of 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.

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

Arguments

  • arg — A string containing any number of hexadecimal digits. String or FixedString

Returned value

Returns a binary string (BLOB). String

Examples

Basic usage

Convert to number