Home Back to 370 Assembler Tips & Tricks Index Links

Simple Binary to Displayable Hex Conversion

If you've ever needed to display a binary data for diagnostic or monitoring programs you can convert the binary data to displayable hexadecimal data with only two instructions.

Over time, I've seen all kinds of techniques involving shifting bits around, lots of CLI's, OR's, etc., etc..

Here's a code snippet illustrating the technique using UNPK and TR:

 


           .
           .
           .
        MVC    HEXBIN,ANYDATA      Move some data to convert
*                                  to HEXBIN.
*
        UNPK   HEXDIS(L'HEXDIS+1),HEXBIN(L'HEXBIN+1)
        TR     HEXDIS,HEXTRT
*                                  Now you can display or print
*                                  HEXDIS.
           .
           .
           .
         CNOP  0,4                 Fullword alignment.
HEXTRT   EQU   *-X'F0'             16 Byte Translate Table.
         SPACE ,
*                   0 1 2 3 4 5 6 7 8 9 A B C D E F
         SPACE ,
         DC    XL16'F0F1F2F3F4F5F6F7F8F9C1C2C3C4C5C6'  F0 - FF
           .
           .
           .
HEXBIN   DS    XL4                 4 Byte Binary Field.
         DS    X                   1 Byte Pad for UNPK.
HEXDIS   DS    CL8                 8 Byte Displayable Hex Field.
         DS    C                   1 Byte Pad for UNPK.
           .
           .
           .

In the above example, we convert a four byte binary field into an eight byte displayable hexadecimal field.

Note the extra byte after the binary and displayable hexadecimal fields.  I typically prefer to convert a word at a time, but you could convert any number of bytes up to 15 at one time.  My recommendation is to stick to converting words (4 bytes) or double-words (8 bytes) at a time.

Also note that we only need the last sixteen bytes of the translate table, thus the "EQU *-X'F0'" since we'll never have a value other than X'F0' through X'FF' after the UNPK instruction.

Follow this link for inverse of this example to convert displayable hexadecimal back to binary.

I will continue to add tips and techniques as time permits.

Please direct any inquiries or problems regarding this web to webmaster@marcsweb.com


Page design, composition and HTML by Marc Niegowski
Copyright © 1998-2012, Marc Niegowski - Connectivity, Inc., All Rights Reserved
23 W. Fourth Street • Media • Pennsylvania • 19063-2805 • USA
Phone: 610-566-0227 • Fax: 610-566-0641 • Email: Marc@Tech-Center.com

Revision Date: Wednesday, November 15, 2006 09:59:23 AM