> For the complete documentation index, see [llms.txt](https://mariadb.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariadb.com/docs/server/reference/sql-functions/string-functions/hex.md).

# HEX

## Syntax

```
HEX(N_or_S)
```

## Description

If `N_or_S` is a number, returns a string representation of the hexadecimal value of `N`, where `N` is a `longlong` ([BIGINT](/docs/server/reference/data-types/numeric-data-types/bigint.md)) number. This is equivalent to [CONV](/docs/server/reference/sql-functions/numeric-functions/conv.md)(N,10,16).

If `N_or_S` is a string, returns a hexadecimal string representation of`N_or_S` where each byte of each character in `N_or_S` is converted to two hexadecimal digits. If `N_or_S` is `NULL`, returns `NULL`. The inverse of this operation is performed by the [UNHEX](/docs/server/reference/sql-functions/string-functions/unhex.md)()\
function.

`HEX()` with an [INET6](/docs/server/reference/data-types/string-data-types/inet6.md) argument returns a hexadecimal representation of the underlying 16-byte binary string.

## Examples

```sql
SELECT HEX(255);
+----------+
| HEX(255) |
+----------+
| FF       |
+----------+

SELECT 0x4D617269614442;
+------------------+
| 0x4D617269614442 |
+------------------+
| MariaDB          |
+------------------+

SELECT HEX('MariaDB');
+----------------+
| HEX('MariaDB') |
+----------------+
| 4D617269614442 |
+----------------+
```

```sql
SELECT HEX(CAST('2001:db8::ff00:42:8329' AS INET6));
+----------------------------------------------+
| HEX(CAST('2001:db8::ff00:42:8329' AS INET6)) |
+----------------------------------------------+
| 20010DB8000000000000FF0000428329             |
+----------------------------------------------+
```

## See Also

* [Hexadecimal literals](/docs/server/reference/sql-structure/sql-language-structure/hexadecimal-literals.md)
* [UNHEX()](/docs/server/reference/sql-functions/string-functions/unhex.md)
* [CONV()](/docs/server/reference/sql-functions/numeric-functions/conv.md)
* [BIN()](/docs/server/reference/sql-functions/string-functions/bin.md)
* [OCT()](/docs/server/reference/sql-functions/numeric-functions/oct.md)

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}
