HEX

HEX returns a character string that is the hexadecimal representation of the storage that contains x.

Read syntax diagramSkip visual syntax diagram
>>-HEX(x-+----+-)----------------------------------------------><
         '-,z-'     

HEX(x) returns a character string of length 2 * size(x).

HEX(x,z) returns a character string that contains x with the character z inserted between every set of eight characters in the output string. Its length is 2 * size(x) + ((size(x) - 1)/4).

Under the compiler option USAGE(HEX(CSTG)), the length used in the above calculations is based, for VARYING, VARYING4, and VARYINGZ strings, on cstg(x) rather than on stg(x).

x
Expression that represents any variable. The whole number of bytes that contain x is converted to hexadecimal.
z
Expression. If specified, z must have the type CHARACTER(1) NONVARYING.

Integer, offset and pointer values will be presented in bigendian form.

If the number of bytes to be converted to hex is not known at compile time, then no more than 32767 bytes will be converted.

Note: This function does not return an exact image of x in storage. If an exact image is required, use the HEXIMAGE built-in function.

Example 1

  dcl Sweet char(5) init('Sweet');
  dcl Sixteen fixed bin(31) init(16) littleendian;
  dcl XSweet char(size(Sweet)*2+(size(Sweet)-1)/4);
  dcl XSixteen char(size(Sixteen)*2+(size(Sixteen)-1)/4);

  XSweet = hex(Sweet,'-');
             /* '53776565-74' */

  XSweet = heximage(addr(Sweet),length(Sweet),'-');
             /* '53776565-74' */

  XSixteen = hex(Sixteen,'-');
             /* '00000010' - bytes reversed */

  XSixteen = heximage(addr(Sixteen),stg(Sixteen),'-');
             /* '10000000' - bytes NOT reversed */

Example 2

  dcl X fixed bin(15) littleendian;
  dcl Y fixed bin(15) bigendian;

  X = 258;                                       /* stored as '0201'B4 */
  Y = 258;                                       /* stored as '0102'B4 */

  display (hex(X));                              /* displays 0102 */
  display (hex(Y));                              /* displays 0102 */

  display (heximage( addr(X), stg(X) ));         /* displays 0201 */
  display (heximage( addr(Y), stg(Y) ));         /* displays 0102 */





Published: 23 December 2018