Converts a RAW value into a character string consisting of an even number of hexadecimal digits (0 through 9 and A through F).

Syntax

HEX-ENCODE( expression )
expression
A RAW expression containing the value you want to convert. If the expression is the Unknown value (?), the result is the Unknown value (?). If the expression is a zero-length value, the result is a zero-length value.
Note: The BASE64-ENCODE, BASE64-DECODE, HEX-ENCODE, and HEX-DECODE functions do not do any byte ordering. However, the binary output of these functions may be a binary data type such as a MEMPTR or RAW. If the target of these operation’s output is another system where the order of bytes (endianness) differs, you must first normalize the data to what the receiving side is expecting. Otherwise, the results may not be as expected. For example, for a MEMPTR value that you are building yourself, you can use the SET-BYTE-ORDER statement to set the byte order for the MEMPTR variable before you read or write the value.

Example

The following code example illustrates how to use the HEX-ENCODE function:

DEFINE VARIABLE memValue AS MEMPTR NO-UNDO.
DEFINE VARIABLE counter AS INTEGER NO-UNDO.
DEFINE VARIABLE hexString AS CHARACTER NO-UNDO.

/* Allocate 5 bytes in the MEMPTR and populate them with "Hello" (ASCII) */
SET-SIZE(memValue) = 5.

PUT-BYTE(memValue, 1) = 72.  /* H */
PUT-BYTE(memValue, 2) = 101. /* e */
PUT-BYTE(memValue, 3) = 108. /* l */
PUT-BYTE(memValue, 4) = 108. /* l */
PUT-BYTE(memValue, 5) = 111. /* o */

/* Output the byte values in the MEMPTR that will be encoded */
DO counter = 1 TO GET-SIZE(memValue):
  MESSAGE "Byte" counter ":" GET-BYTE(memValue, counter).
END.

/* Encode the MEMPTR bytes as a hex string and display the result */
hexString = HEX-ENCODE(memValue).
MESSAGE "HEX-ENCODE result:" hexString.

Running this code produces a sequence of messages showing the byte values: 72, 101, 108, 108, 111, and then a final message with HEX-ENCODE result: 48656C6C6F.

See also

HEX-DECODE function, SET-BYTE-ORDER statement