# 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
```
