---
schema_version: "1"
id: f4f5e327c6df41641430ce1d7964b629e96bf325063b0038c6e401111ec2e2e7
title: StringToHex()
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/marketing-cloud/ampscript/references/string/mc-ampscript-reference-string-to-hex.md
canonical_url: https://developer.salesforce.com/docs/marketing/marketing-cloud-ampscript/references/mc-ampscript-string/mc-ampscript-reference-string-to-hex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 409f086ea7bd27a894fe414aaf59a3773670c01b03d46f58e6a8a32bda734540
products:
  - AMPscript for Marketing Cloud
  - Marketing Cloud
---
# StringToHex()

Converts each character in a string to its hexadecimal equivalent.

## Availability

<table>
  <tr>
    <td>Marketing Cloud Engagement</td>
    <td>✅ Yes</td>
  </tr>
  <tr>
    <td>Marketing Cloud Next</td>
    <td>✅ Yes</td>
  </tr>
</table>

## Syntax

```
StringToHex(sourceString, charSet)
```

The `StringToHex()` function has two parameters:

- `sourceString` (string): Required. The string to convert to hexadecimal character codes.
- `charSet` (string): The character set to use for encoding. Accepted values are `UTF-8` and `UTF-16`. The default value is `UTF-8`.

## Usage

To use the function, pass it a string to encode and optionally a character set for encoding.

```
%%[
  Var @sourceString, @hexStringUtf8, @hexStringUtf16
  Set @sourceString = "Hello world!"
  Set @hexStringUtf8 = StringToHex(@sourceString)
  Set @hexStringUtf16 = StringToHex(@sourceString, 'UTF-16')
]%%
<p>Source string: %%=v(@sourceString)=%%</p>
<p>UTF-8 hex string: %%=v(@hexStringUtf8)=%%</p>
<p>UTF-16 hex string: %%=v(@hexStringUtf16)=%%</p>
```

The example returns the source string, the UTF-8 hex string, and the UTF-16 hex string.

```
Source string: Hello world!
UTF-8 hex string: 48656c6c6f20776f726c6421
UTF-16 hex string: 480065006c006c006f00200077006f0072006c0064002100
```
