This topic describes the syntax of binary functions. This topic also provides examples on how to use the functions.
The following table describes the binary functions that are supported by Log Service.
- If you want to use strings in analytic statements, you must enclose the strings in single quotation marks (''). Strings that are not enclosed or enclosed in double quotation marks ("") indicate field names or column names. For example, 'status' indicates the status string, and status or "status" indicates the status log field.
- varbinary is a binary character type, and varchar is a variable-length character type.
Function | Syntax | Description |
---|---|---|
from_base64 function | from_base64(x) | Converts a Base64-encoded string to a binary number. |
from_base64url function | from_base64url(x) | Converts a Base64-encoded string to a binary number by using URL reserved characters. |
from_big_endian_64 function | from_big_endian_64(x) | Converts a binary number in big endian mode to a bigint value. |
from_hex function | from_hex(x) | Converts a hexadecimal number to a binary number. |
length function | length(x) | Calculates the length of a binary number. |
md5 function | md5(x) | Computes the MD5 hash value for a binary number. |
to_base64 function | to_base64(x) | Converts a binary number to a Base64-encoded string. |
to_base64url function | to_base64url(x) | Converts a binary number to a Base64-encoded string by using URL reserved characters. |
to_hex function | to_hex(x) | Converts a binary number to a hexadecimal number. |
to_big_endian_64 function | to_big_endian_64(x) | Converts a bigint value to a binary number in big endian mode. |
sha1 function | sha1(x) | Computes the SHA-1 hash value for a binary number. |
sha256 function | sha256(x) | Computes the SHA-256 hash value for a binary number. |
sha512 function | sha512(x) | Computes the SHA-512 hash value for a binary number. |
xxhash64 function | xxhash64(x) | Computes the xxhash64 hash value for a binary number. |
from_base64 function
The from_base64 function is used to convert a Base64-encoded string to a binary number.
Syntax
from_base64(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varbinary type.
- If the returned binary number is an invisible character, you can use the to_hex function to convert the number to a hexadecimal number.
- If the returned binary number is a visible character, you can use the from_utf8 function to convert the number to a UTF-8 string.
Examples
Convert a Base64-encoded string to a binary number and then convert the binary number to a hexadecimal number.
- Query statement
* | SELECT to_hex(from_base64('c2xz'))
- Query and analysis result
from_base64url function
The from_base64url function is used to convert a Base64-encoded string to a binary number by using URL reserved characters.
Syntax
from_base64url(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
- If the returned binary number is an invisible character, you can use the to_hex function to convert the number to a hexadecimal number.
- If the returned binary number is a visible character, you can use the from_utf8 function to convert the number to a UTF-8 string.
Examples
Convert a Base64-encoded string to a binary number by using URL reserved characters.
- Query statement
* | SELECT to_hex(from_base64url('c2xz'))
- Query and analysis result
from_big_endian_64 function
The from_big_endian_64 function is used to convert a binary number in big endian mode to a bigint value.
Syntax
from_big_endian_64(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The bigint type.
Examples
Convert the binary number 10 in big endian mode to a bigint value.
- Query statement
* | SELECT from_big_endian_64(to_big_endian_64(10))
- Query and analysis result
from_hex function
The from_hex function is used to convert a hexadecimal number to a binary number.
Syntax
from_hex(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the varbinary type. |
Return value type
The varbinary type.
Examples
Convert the hexadecimal number D74D to a binary number.
- Query statement
* | SELECT from_hex('D74D')
- Query and analysis result
length function
The length function is used to calculate the length of a binary number.
Syntax
length(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The bigint type.
Examples
Calculate the length of the region field value.
- Query statement
* | SELECT length('00101000')
- Query and analysis result
md5 function
The md5 function is used to compute the MD5 hash value for a binary number.
Syntax
md5(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the varbinary type. |
Return value type
The varbinary type.
Examples
Compute the MD5 hash value for the binary number 1101.
- Query statement
* | SELECT MD5(from_base64('1101')) AS md5
- Query and analysis result
to_base64 function
The to_base64 function is used to convert a binary number to a Base64-encoded string.
Syntax
to_base64(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varchar type.
Examples
Convert the binary number 10 to a Base64-encoded string.
- Query statement
* | SELECT to_base64(from_base64('10')) AS base64
- Query and analysis result
to_base64url function
The to_base64url function is used to convert a binary number to a Base64-encoded string by using URL reserved characters.
Syntax
to_base64url(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varchar type.
Examples
Convert the binary number 100 to a Base64-encoded string by using URL reserved characters.
- Query statement
* | SELECT to_base64url(from_base64('100'))
- Query and analysis result
to_hex function
The to_hex function is used to convert a binary number to a hexadecimal number.
Syntax
to_hex(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varchar type.
Examples
Convert the binary number 100 to a hexadecimal number.
- Query statement
* | SELECT to_hex(from_base64('100'))
- Query and analysis result
to_big_endian_64 function
The to_big_endian_64 function is used to convert a bigint value to a binary number in big endian mode.
Syntax
to_big_endian_64(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the bigint type. |
Return value type
The varbinary type.
Examples
Convert the bigint value 0 to a binary number in big endian mode.
- Query statement
* | SELECT to_big_endian_64(0)
- Query and analysis result
sha1 function
The sha1 function is used to compute the SHA-1 hash value for a binary number.
Syntax
sha1(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varbinary type.
Examples
Compute the SHA-1 hash value for the binary number 1101.
- Query statement
* | SELECT sha1(from_base64('1101')) AS sha1
- Query and analysis result
sha256 function
The sha256 function is used to compute the SHA-256 hash value for a binary number.
Syntax
sha256(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varbinary type.
Examples
Compute the SHA-256 hash value for the binary number 1101.
- Query statement
* | SELECT sha256(from_base64('1101')) AS sha256
- Query and analysis result
sha512 function
The sha512 function is used to compute the SHA-512 hash value for a binary number.
Syntax
sha512(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varbinary type.
Examples
Compute the SHA-512 hash value for the binary number 1101.
- Query statement
* | SELECT sha512(from_base64('1101')) AS sha512
- Query and analysis result
xxhash64 function
The xxhash64 function is used to compute the xxhash64 hash value for a binary number.
Syntax
xxhash64(x)
Parameters
Parameter | Description |
---|---|
x | The value of this parameter is of the binary type. |
Return value type
The varbinary type.
Examples
Compute the xxhash64 hash value for the binary number 10.
- Query statement
* | SELECT xxhash64(from_base64('10'))
- Query and analysis result