StringToHex Function

The StringToHex function converts a string value to hexadecimal encoding. This is done by converting the 8 bit data to 2 hex characters. The hex characters are then stored as the two byte string representation of the characters. Related functions are Base64Encode and Base64Decode.

Syntax:

                    

StringToHex(1,2)
1 = String value (can be a variable)
2 = Character set for encoding. Defaults to UTF-8 but can use UTF-16

                

Example:

                    

%%[ 
var @string
set @string = “Hexadecimal encoding of a string”
]%%
%%=StringToHex(@string, “UTF-16”)=%%

                

Output:

480065007800610064006500630069006d0061006c00200065006e0063006f00640069006e00670020006f00660020006100200073007400720069006e006700

Explanation:

In the AMPscript above, first we are declaring the variables using the var function. Then defining the @string variable as “Hexadecimal encoding of a string”

The output of that value is the UTF-16 character set. If the UTF-16 were omitted, the output would default to the UTF-8 unicode standard.

Output (UTF-8):

48657861646563696d616c20656e636f64696e67206f66206120737472696e67

Leave a Reply

Your email address will not be published. Required fields are marked *