• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • Guru: Legible Hexadecimal

    August 28, 2017 Ted Holt

    Since I am a normal red-blooded human being, I try to make life as easy as possible for myself. I don’t do very well in general, but occasionally I manage to afford myself a bit of comfort. Recently I had to deal with long strings of hexadecimal digits. Trying to read that stuff was more than I could deal with, so I wrote a function to help me.

    It’s easy to think of hexadecimal literal as strings because they contain the letters A through F. However, they are not strings, but numbers. We use the letters A through F for the highest digits of the base 16 number system, but we could just as easily use any other six symbols, even symbols that no one has invented.

    To make decimal numbers easier to read, we use editing characters. In the United States of America, those editing characters would be the period, which serves as a decimal point, and the comma, which separates groups of three digits left of the decimal point. Other nations have other conventions.

    In the same way, we can edit hexadecimal numbers. One common method of hexadecimal editing is to divide the hex literal into groups of two hex digits. Each group represents one byte. I wrote a SQL function to display a hexadecimal number in that format. I call it LegibleHex.

    Here’s the way the SQL HEX function, which comes as part of DB2, displays the hexadecimal representation of a character value.

    select lstnam, hex(lstnam) from qiws.qcustcdt
    
    LSTNAM    HEX ( LSTNAM ) 
    ========  ================ 
    Henning   C885959589958740
    Jones     D1969585A2404040
    Vine      E589958540404040
    Johnson   D1968895A2969540
    Tyron     E3A8999695404040
    Stevens   E2A385A58595A240
    Alison    C19389A296954040
    Doe       C496854040404040
    Thomas    E388969481A24040
    Williams  E6899393898194A2
    Lee       D385854040404040
    Abraham   C182998188819440
    

    This isn’t terrible, as the last name field is only eight bytes long. In my application, I was dealing with a 40-byte character field, which yielded 80 hexadecimal digits.

    Here’s the same output using the LegibleHex function:

    select lstnam, legiblehex(trim(lstnam))
      from qiws.qcustcdt
    
    LSTNAM    LEGIBLEHEX
    ========  ========================
    Henning   C8 85 95 95 89 95 87
    Jones     D1 96 95 85 A2
    Vine      E5 89 95 85
    Johnson   D1 96 88 95 A2 96 95
    Tyron     E3 A8 99 96 95
    Stevens   E2 A3 85 A5 85 95 A2
    Alison    C1 93 89 A2 96 95
    Doe       C4 96 85
    Thomas    E3 88 96 94 81 A2
    Williams  E6 89 93 93 89 81 94 A2
    Lee       D3 85 85
    Abraham   C1 82 99 81 88 81 94
    

    That’s quite an improvement. Now I can tell where one byte begins and another ends. I further improved legibility by chopping off the trailing blanks.

    If you would like to use this function, have at it. Here’s the source code, written in my newest favorite language, SQL PL.

    create or replace function LegibleHex
       (p_Value  varchar(4096))
       returns varchar(12288)
       deterministic
       returns null on null input
       not fenced
    
    begin
       declare v_Length     integer;
       declare v_Index      integer;
       declare v_Work       varchar(8192);
       declare v_Output     varchar(12288)  default '';
    
       set v_Work = hex(p_Value);
       set v_Length = length(v_Work);
    
       set v_Index = 1;
    
       while v_Index < v_Length do
          set v_Output = v_Output concat
                         substr(v_Work, v_Index, 2) concat ' ';
          set v_Index = v_Index + 2;
       end while;
       return v_Output;
    end;
    

    Paste the source code into the source physical file member or stream file of your choice. Use the Change Current Library (CHGCURLIB) command to designate the library in which you want the function to reside. Then use the Run SQL Statements (RUNSQLSTM) command to execute the CREATE FUNCTION command. If you’re not sure how to do that, click here.

    A proverb of disputed origin says that it is better to light a candle than to curse the darkness. I heartily concur. Consider the LegibleHex function a lit candle.

    RELATED STORY

    The Three Sources Of RUNSQLSTM

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags: Tags: 400guru, hexadecimal, IBM i

    Sponsored by
    VISUAL LANSA 16 WEBINAR

    Trying to balance stability and agility in your IBM i environment?

    Join this webinar and explore Visual LANSA 16 – our enhanced professional low-code platform designed to help organizations running on IBM i evolve seamlessly for what’s next.

    🎙️VISUAL LANSA 16 WEBINAR

    Break Monolithic IBM i Applications and Unlock New Value

    Explore modernization without rewriting. Decouple monolithic applications and extend their value through integration with modern services, web frameworks, and cloud technologies.

    🗓️ July 10, 2025

    ⏰ 9 AM – 10 AM CDT (4 PM to 5 PM CEST)

    See the webinar schedule in your time zone

    Register to join the webinar now

    What to Expect

    • Get to know Visual LANSA 16, its core features, latest enhancements, and use cases
    • Understand how you can transition to a MACH-aligned architecture to enable faster innovation
    • Discover native REST APIs, WebView2 support, cloud-ready Azure licensing, and more to help transform and scale your IBM i applications

    Read more about V16 here.

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    As I See It: So You’re Thinking About Retirement Syncsort CEO Discusses Vision Deal, Product Plans

    One thought on “Guru: Legible Hexadecimal”

    • Russ K says:
      August 29, 2017 at 10:11 am

      Hi Ted, another interesting article as usual. Regarding the CHGCURLIB, I’ve been putting some CL commands directly in my SQL so I don’t have to remember to do things manually. For this one, I put a line above the create function statement: CL: CHGCURLIB mylib;
      Yes, it’s hard-coding but there are benefits. Thanks for all the great tips.

      Reply

    Leave a Reply Cancel reply

TFH Volume: 27 Issue: 54

This Issue Sponsored By

  • Fresche Solutions
  • RPG & DB2 Summit
  • HiT Software, Inc. a BackOffice Associates Company
  • Computer Keyes
  • WorksRight Software

Table of Contents

  • Why Not Overclock Power Chips For IBM i?
  • Syncsort CEO Discusses Vision Deal, Product Plans
  • Guru: Legible Hexadecimal
  • As I See It: So You’re Thinking About Retirement
  • Investing In IBM i: Adventures In Venture Capital

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • Liam Allan Shares What’s Coming Next With Code For IBM i
  • From Stable To Scalable: Visual LANSA 16 Powers IBM i Growth – Launching July 8
  • VS Code Will Be The Heart Of The Modern IBM i Platform
  • The AS/400: A 37-Year-Old Dog That Loves To Learn New Tricks
  • IBM i PTF Guide, Volume 27, Number 25
  • Meet The Next Gen Of IBMers Helping To Build IBM i
  • Looks Like IBM Is Building A Linux-Like PASE For IBM i After All
  • Will Independent IBM i Clouds Survive PowerVS?
  • Now, IBM Is Jacking Up Hardware Maintenance Prices
  • IBM i PTF Guide, Volume 27, Number 24

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle