> For the complete documentation index, see [llms.txt](https://cortex-docs.paloaltonetworks.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/convert_to_base_64.md).

# convert\_to\_base\_64

Use the `convert_to_base_64()` function to encode an input string into its base64 representation.

## Syntax

```sql
convert_to_base_64 ("<input_string>")
```

## Parameters

| Name           | Type   | Required | Description                      |
| -------------- | ------ | -------- | -------------------------------- |
| `input_string` | string | Yes      | The string to be base64 encoded. |

## Returns

The `convert_to_base_64()` function returns the base64-encoded value as a string.

## Usage notes

* The function requires a single string input.
* The function will return `NULL` if the input is `NULL`.
* This function is typically employed within the `alter` stage to create new fields or modify existing ones by encoding string data.
* The function can also be used within a `filter` stage if the encoding result is needed for a conditional check.

## Examples

### Example 1: Encoding a basic literal string

**Goal**: Encode a simple literal string to its base64 representation.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter encoded_string = convert_to_base_64("Hello world")
| fields event_id, encoded_string
| limit 3
```

**Explanation**: This query adds a new field, `encoded_string`, which contains the base64-encoded representation of "Hello world".

**Output**:

| EVENT\_ID | ENCODED\_STRING  |
| --------- | ---------------- |
| 101       | SGVsbG8gd29ybGQ= |
| 102       | SGVsbG8gd29ybGQ= |
| 103       | SGVsbG8gd29ybGQ= |

### Example 2: Encoding an existing string field

**Goal**: Encode values from an existing string field in the dataset.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter encoded_description = convert_to_base_64(event_description)
| fields event_id, event_description, encoded_description
| limit 3
```

**Explanation**: The query creates `encoded_description` by applying base64 encoding to the `event_description` field for each record.

**Output**:

| EVENT\_ID | EVENT\_DESCRIPTION             | ENCODED\_DESCRIPTION                     |
| --------- | ------------------------------ | ---------------------------------------- |
| 101       | User login successful          | VXNlciBsb2dpbiBzdWNjZXNzZnVs             |
| 102       | File access attempt            | RmlsZSBhY2Nlc3MgYXR0ZW1wdA==             |
| 103       | Network connection established | TmV0d29yayBjb25uZWN0aW9uIGVzdGFibGlzaGVk |

### Example 3: Encoding a string derived from a numeric field

**Goal**: Convert a numeric field to a string and then encode it to base64.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter event_id_string = to_string(event_id)
| alter encoded_id = convert_to_base_64(event_id_string)
| fields event_id, event_id_string, encoded_id
| limit 3
```

**Explanation**: The `event_id` is first converted to a string using `to_string()`, and then `convert_to_base_64()` encodes this string.

**Output**:

| EVENT\_ID | EVENT\_ID\_STRING | ENCODED\_ID |
| --------- | ----------------- | ----------- |
| 101       | 101               | MTAx        |
| 102       | 102               | MTAy        |
| 103       | 103               | MTAz        |

### Example 4: Encoding an empty string

**Goal**: Encode an empty string to observe the result.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter encoded_empty_string = convert_to_base_64("")
| fields event_id, encoded_empty_string
| limit 3
```

**Explanation**: Encoding an empty string results in an empty string.

**Output**:

| EVENT\_ID | ENCODED\_EMPTY\_STRING |
| --------- | ---------------------- |
| 101       |                        |
| 102       |                        |
| 103       |                        |

### Example 5: Handling NULL input

**Goal**: Observe the behavior when a NULL input is provided to the function.

**XQL code**:

```sql
config timeframe = 1d
| dataset = sample_xql_raw
| alter encoded_null_field = convert_to_base_64(dst_domain)
| fields event_id, dst_domain, encoded_null_field
| limit 5
```

**Explanation**: When the input to `convert_to_base_64()` is `NULL` (such as `dst_domain` for event 105), the function consistently returns `NULL` for the output field.

**Output**:

| EVENT\_ID | DST\_DOMAIN                             | ENCODED\_NULL\_FIELD     |
| --------- | --------------------------------------- | ------------------------ |
| 101       | ec2.amazonaws.com                       | ZWMyLmFtYXpvbmF3cy5jb20= |
| 102       | sts.amazonaws.com                       | c3RzLmFtYXpvbmF3cy5jb20= |
| 103       | [www.google.com](http://www.google.com) | d3d3Lmdvb2dsZS5jb20=     |
| 104       | dropbox.com                             | ZHJvcGJveC5jb20=         |
| 105       | NULL                                    | NULL                     |

## Related articles

* **Stages**: [`alter`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/alter.md), [`fields`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/stages/fields.md)
* **Functions**: [`convert_from_base_64`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/convert_from_base_64.md), [`to_string`](https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/to_string.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://cortex-docs.paloaltonetworks.com/xql-command-reference-guide/readme/functions/convert_to_base_64.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
