Published on

HEX-OF

When to use COBOL function HEX-OF

The HEX-OF function is designed to return an alphanumeric character string, representing the bytes of the input argument in a hexadecimal format. Here's the basic format:

FUNCTION HEX-OF (argument-1)

Usage and Characteristics:

  • argument-1: This can be a data item, literal, or the result of any intrinsic function. It serves as the source character string for the conversion.

  • The function returns an alphanumeric character string, and its length, in bytes, is two times the length of argument-1.

  • Note: If argument-1 is invalid, the behavior is undefined.

Examples:

Let's look at a few examples to grasp the practical application of HEX-OF:

Converting a literal string:

FUNCTION HEX-OF('Hello, world!') returns 'C8859393966B40A6969993845A'

Converting a BINARY data item:

01 BIN PIC 9(9) BINARY VALUE 12.
FUNCTION HEX-OF(BIN) returns '0000000C'

Converting a COMP-3 (Packed-Decimal) data item:

01 PAC PIC 9(5) COMP-3 VALUE 12345.
FUNCTION HEX-OF(PAC) returns '12345F'

Converting a numeric data item:

01 ZON PIC 9(5) VALUE 12345.
FUNCTION HEX-OF(ZON) returns 'F1F2F3F4F5'

Leveraging with NATIONAL-OF function:

FUNCTION HEX-OF(FUNCTION NATIONAL-OF(' ')) returns '0020'

Conclusion:

The HEX-OF function in COBOL adds a versatile tool for handling hexadecimal representations of data. Whether dealing with strings or numeric values, this function provides a concise and efficient way to work with hexadecimal formats. As always, exercise caution to ensure the validity of the input, as undefined behavior may arise with invalid arguments.