

# Blob and bitstring functions in DuckDB

Use blob and bitstring functions to work with binary data. A `BLOB` value holds a sequence of bytes, and a `BIT` value, which you can also write as `BITSTRING`, holds a sequence of bits. This category also has the hash functions `md5`, `md5_number`, `md5_number_lower`, `md5_number_upper`, `sha1`, and `sha256`. MD5 and SHA-1 are not secure for cryptographic use, so do not use the MD5 and SHA-1 functions to protect data. When you need a secure hash, use the [`sha256` function](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-sha256).

The category also has the `repeat` function, which repeats strings, blobs, and lists. To read files as binary data, use the `read_blob` table function. For more information about `read_blob`, see [`read_blob` function](duckdb-functions-table-file-readers.md#athena-duckdb-functions-table-read-blob).

To write a `BLOB` literal, cast a string to `BLOB`, as in `'\xAA\xBB'::BLOB`. Each `\x` followed by two hexadecimal digits stands for one byte, and each other ASCII character stands for its own byte. A cast to `BLOB` fails for a string that holds a character outside the ASCII range, such as `chr(233)`. To get the UTF-8 bytes of such a string, use `encode` instead. To write a `BIT` literal, cast a string of `0` and `1` characters, as in `'1011'::BIT`. To convert text to the bytes of its UTF-8 encoding and back, use the [`encode` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-encode) and the [`decode` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-decode). To convert a `BLOB` to or from base64 text, use the [`to_base64` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-to-base64) and the [`from_base64` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-from-base64). To write a value as hexadecimal or binary digits, use the [`hex` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-hex) or the [`bin` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-bin). To turn such digits back into a `BLOB`, use the [`unhex` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unhex) or the [`unbin` function](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unbin).

Query results show a `BLOB` value as text. Bytes that are letters, digits, and most other printable ASCII characters appear as those characters, and other bytes appear as `\x` followed by two hexadecimal digits, for example `\xAA`. A `BIT` value appears as a string of `0` and `1` characters.

Hive and Iceberg tables cannot store `BIT` values. To store a bitstring in a table, cast it to `VARCHAR`, as in `CAST({{bits}} AS VARCHAR)`.

**Topics**
+ [Encoding](duckdb-functions-binary-blobs-and-encoding.md)
+ [Hashing](duckdb-functions-binary-hashing.md)
+ [Bitstrings](duckdb-functions-binary-bitstrings.md)

The following table lists the blob and bitstring functions, with a link to the description of each one.


| Function | Description | 
| --- | --- | 
| base64 | Alias of [to\_base64](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-to-base64). | 
| [bin](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-bin) | Returns the binary digits of an integer, or of the bytes of a string, as a string. | 
| [bit\_count](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-bit-count) | Counts the `1` bits in an integer or a bitstring. | 
| [bit\_length](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-bit-length) | Returns the number of bits in a string or a bitstring. | 
| [bit\_position](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-bit-position) | Returns the position of the first occurrence of one bitstring inside another. | 
| [bitstring](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-bitstring) | Returns a bitstring widened to a given number of bits, with `0` bits added on the left. | 
| [decode](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-decode) | Reads the bytes of a `BLOB` as UTF-8 text and returns a string. | 
| [encode](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-encode) | Returns the UTF-8 bytes of a string as a `BLOB`. | 
| [from\_base64](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-from-base64) | Decodes a base64-encoded string and returns the bytes as a `BLOB`. | 
| from\_binary | Alias of [unbin](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unbin). | 
| from\_hex | Alias of [unhex](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unhex). | 
| [get\_bit](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-get-bit) | Returns the bit at a given position in a bitstring. | 
| [hash](duckdb-functions-utility-sort-keys-bins-hashes.md#athena-duckdb-functions-utility-hash) | Returns a 64-bit hash of one or more values of any type. It is not a cryptographic hash. The utility functions topic describes it. | 
| [hex](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-hex) | Converts an integer, a string, or a blob to hexadecimal text. | 
| [md5](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-md5) | Hashes a string or a blob with MD5 and returns the hash as hexadecimal text. | 
| [md5\_number](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-md5-number) | Hashes a string or a blob with MD5 and returns the hash as an unsigned 128-bit integer. | 
| [md5\_number\_lower](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-md5-number-lower) | Returns the most significant 64 bits of the 128-bit MD5 hash number of a value. | 
| [md5\_number\_upper](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-md5-number-upper) | Returns the least significant 64 bits of the 128-bit MD5 hash number of a value. | 
| [octet\_length](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-octet-length) | Returns the size of a blob or a bitstring in bytes. | 
| [repeat](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-repeat) | Joins a given number of copies of a string, a blob, or a list into one value, or returns a value in a given number of rows. | 
| [set\_bit](duckdb-functions-binary-bitstrings.md#athena-duckdb-functions-binary-set-bit) | Returns a copy of a bitstring with the bit at a given position set to `0` or `1`. | 
| [sha1](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-sha1) | Hashes a string or a blob with SHA-1 and returns the hash as hexadecimal text. | 
| [sha256](duckdb-functions-binary-hashing.md#athena-duckdb-functions-binary-sha256) | Hashes a string or a blob with SHA-256 and returns the hash as hexadecimal text. | 
| [to\_base64](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-to-base64) | Encodes the bytes of a `BLOB` as a base64 string. | 
| to\_binary | Alias of [bin](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-bin). | 
| to\_hex | Alias of [hex](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-hex). | 
| [unbin](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unbin) | Decodes a string of binary digits and returns the bytes as a `BLOB`. | 
| [unhex](duckdb-functions-binary-blobs-and-encoding.md#athena-duckdb-functions-binary-unhex) | Decodes a string of hexadecimal digits and returns the bytes as a `BLOB`. | 