AMPscript StringToHex (Next)
Convert a string to hexadecimal with AMPscript StringToHex in Marketing Cloud Next. Syntax, examples, and behaviour proven in a Marketing Cloud Next email, including which character sets Next accepts.
Syntax
StringToHex(sourceString[, charSet]) → string
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
sourceString |
string | Yes | The string to render as hexadecimal; the empty string yields the empty string |
charSet |
string | No | Name of the character encoding applied before conversion; defaults to UTF-8. Next accepts only UTF-8 and UTF-16; other names are rejected at save |
Example
%%=StringToHex("SFMC AMPscript 2026")=%%
Renders 53464d4320414d507363726970742032303236.
Return value
string — lowercase hexadecimal digits with no separators, two per byte of the encoded input.
There is no error token to test for: every accepted call returns a hex string.
Behaviour
Lowercase, no separators. StringToHex("A") gives 41, and Length(StringToHex("SFMC AMPscript 2026")) gives 38, two digits for each of the 19 characters.
UTF-8 bytes by default. StringToHex("café€") gives 636166c3a9e282ac: é takes two bytes and € three. Naming "UTF-8" gives the same.
UTF-16 is little-endian. StringToHex("café€", "UTF-16") gives 630061006600e900ac20.
No other character set is accepted. An email that passes UTF-16BE, UTF-32, ASCII, ISO-8859-1 or banana cannot be saved: the content API answers HTTP 400 with “The AMPscript syntax isn’t valid”.
The empty string yields the empty string.
Wrong argument counts are refused at save. StringToHex() and a call with three arguments are rejected the same way.
Show Next test script
<!-- Marketing Cloud Next test script for StringToHex. Paste it into the HTML component of a
Next email and open the preview. Each line prints the result Next gave when the call
was rendered on its own (expect) next to the result of this run (got).
Assembled from single-call test emails (dates on the page); it has not been
run as one email. Calls that make Next fail are listed separately. -->
<p>c1 expect=[41] got=[%%=StringToHex("A")=%%]</p>
<p>c2 expect=[53464d4320414d507363726970742032303236] got=[%%=StringToHex("SFMC AMPscript 2026")=%%]</p>
<p>c3 expect=[] got=[%%=StringToHex("")=%%]</p>
<p>c4 expect=[38] got=[%%=Length(StringToHex("SFMC AMPscript 2026"))=%%]</p>
<p>c5 expect=[636166c3a9e282ac] got=[%%=StringToHex("café€")=%%]</p>
<p>c6 expect=[636166c3a9e282ac] got=[%%=StringToHex("café€","UTF-8")=%%]</p>
<p>c7 expect=[630061006600e900ac20] got=[%%=StringToHex("café€","UTF-16")=%%]</p>
Show calls that fail in Next (one email each)
<!-- Run each call below in its own Next email: one of them in a combined email
would stop the whole preview or block the save. -->
<!-- email 1: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","UTF-16BE")=%%
<!-- email 2: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","UTF-32")=%%
<!-- email 3: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","ASCII")=%%
<!-- email 4: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","ISO-8859-1")=%%
<!-- email 5: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","banana")=%%
<!-- email 6: expect that the save is rejected (HTTP 400) -->
%%=StringToHex()=%%
<!-- email 7: expect that the save is rejected (HTTP 400) -->
%%=StringToHex("café€","UTF-8","extra")=%%
Differs from official docs
- Available in Next. The official reference marks StringToHex as not available in Marketing Cloud Next. It saved and rendered correctly in a Next email (see the examples above). Details
Availability & differences
| Platform | Available |
|---|---|
| Marketing Cloud Engagement | Yes |
| Marketing Cloud Next | Yes, from API 68.0 |
Engagement also accepts UTF-16BE, UTF-32, ASCII and ISO-8859-1 as the character set (for café€, for example, 00630061006600e920ac and 6361663f3f); Next rejects an email with any of them at save. The default UTF-8 and UTF-16 give the same hex on both. Compared 2026-11-23 by rendering the same calls in a Marketing Cloud Next email.
See also
- Base64Encode — the same bytes in Base64
- Base64Decode
- SHA256 — hashes bytes chosen by a similar character-set argument
- Official reference