Can COBOL Display a Hex Value?


COBOL, the Common Business-Oriented Language, has been a mainstay of commercial software programmers since its origins in 1959. Though COBOL more easily handles standard numeric and text data formats, you can also use it to display hexadecimal values. Hexadecimal numbers, or hex, are a base-16 format used to represent all data inside a computer. Hex is handy for debugging programs; using it, you can identify the exact contents of computer memory.

Hex Format

  • Computer engineers adopted the hexadecimal, or base-16, numbering system as a convenient way to represent computer bits. Instead of writing and displaying individual bits, a computer displays hex digits that stand for four bits each. Each hex digit takes on 16 different values; because 10 decimal numbers are insufficient to express this value, hex digits use the numbers zero through nine plus the letters A to F. You can more easily use the hex number “A081” than the binary equivalent “1010000010000001,” for example.

Hex Literal

  • In COBOL as in other languages, literal values are useful to initialize data areas, reset variables and perform many other housekeeping tasks. For example, you set the heading of a shipping report to the literal text value, “JONES INC. WEEKLY SHIPPING.” On occasion, you may have to set a variable to a hex number; to do this, begin the value with an “X”, as the following COBOL statements show:

      05 HEX-CODE   PIC X(01) VALUE X”2D”.



    Note that quotation marks surround the hex value. Using this technique, you see a variable’s value in hex, though only in a program’s source code listing.


  • To identify the cause of broken code, programmers use a software tool called a debugger. The debugger allows the programmer to see exactly how the program executes and lets her see the contents of the program’s memory variables. A good debugger lets you see memory contents in different formats including text, numbers and hex. Though somewhat harder to read than familiar numbers or text, a hex display reveals the exact data stored in memory. By observing how data changes as the program runs, the programmer can find the problem’s cause.

Hex Dump

  • Some versions of COBOL have a feature that generates a listing called a “hex dump” if the program crashes. The hex dump is a hexadecimal display of all the variables in the program’s memory along with a map of where variables reside in memory. Because this works only when the program terminates abnormally, and because it displays all of a program’s memory, you cannot use this technique for displaying a few hex values when running a program normally.

Added Code

  • Most versions of COBOL do not have a built-in function to display a few hex values. Instead, you must add programming code that translates values into hex, then displays them. Such an addition to a program would include a variable defined as an array of single bytes; in the program’s “Procedure Division,” a routine scans the array byte by byte and translates each byte to an equivalent table of text characters. When the program displays the text characters, they have the appearance of hex values. For example, the program translates the character string “ABC” to the equivalent string “C1C2C3.” The first two characters, “C1,” for example, represent the letter “A” in hex.


  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet


Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!