It's a particularly busy week for me: on top of a few looming deadlines, I'll be at Business of Software 2008 in Boston. So, I figured it'd be the perfect opportunity to revisit some classics.

To the Hexth Degree was originally published on March 24th, 2006 and, to this day, it still holds the title of the Worst Hexadecimal Conversion Routine.


I have a policy that I try follow regarding duplicate concepts: I'll only post the concept again if the implementation somehow outdoes the previous. You may have guessed by the title, but today's example is from one of the more complex realms of mathematics and computer science: hexadecimal. Today's example is actually the sixth post of its kind. David H's former colleague now holds the "hex" prize for using no less than 5,000 lines to convert a byte array to hexadecimal, something which could normally be done with a single line of Java code ...

 

public abstract class HexadecimalConstants 
{
  /**
   * The number of hexadecimal characters per byte.
   */
  public static final int NUMBER_OF_HEXADECIMAL_CHARACTERS_PER_BYTE = 2;

  /**
   * The offset of the first bit within the four bits required to represent an
   * hexadecimal character.
   */
  public static final int FIRST_BIT_OFFSET = 1;

  /**
   * The offset of the second bit within the four bits required to represent
   * an hexadecimal character.
   */
  public static final int SECOND_BIT_OFFSET = 2;

  /**
   * The offset of the third bit within the four bits required to represent an
   * hexadecimal character.
   */
  public static final int THIRD_BIT_OFFSET = 3;

  /**
   * The offset of the fourth bit within the four bits required to represent
   * an hexadecimal character.
   */
  public static final int FOURTH_BIT_OFFSET = 4;

  /**
   * The '0' hexidecimal character.
   */
  public static final char ZERO = '0';

  /**
   * The bits that represent the '0' hexidecimal character.
   */
  public static final boolean[] ZERO_BITS = new boolean[]{false, false, false, false};

  /**
   * The '1' hexidecimal character.
   */
  public static final char ONE = '1';

  /**
   * The bits that represent the '1' hexidecimal character.
   */
  public static final boolean[] ONE_BITS = new boolean[]{false, false, false, true};

  /**
   * The '2' hexidecimal character.
   */
  public static final char TWO = '2';

  /**
   * The bits that represent the '2' hexidecimal character.
   */
  public static final boolean[] TWO_BITS = new boolean[]{false, false, true, false};

/* ... snip 150 or so lines  ... */

  /**
   * The 'F' hexidecimal character.
   */
  public static final char F = 'F';

  /**
   * The 'f' lower case alternative to the 'F' hexidecimal character.
   */
  public static final char F_LOWER = 'f';

  /**
   * The bits that represent the 'F' hexidecimal character.
   */
  public static final boolean[] F_BITS = new boolean[]{true, true, true, true};

}

And a quick peek inside the bowels of the main conversion class ...

 

private static char convertBitsToHexadecimalCharacter(boolean bit1,
                                                      boolean bit2,
                                                      boolean bit3,
                                                      boolean bit4) {

    // if the first bit is true - the binary nibble is 1???
    if (bit1) {

        // if the second bit is true - the binary nibble is 11??
        if (bit2) {

            // if the third bit is true  - the binary nibble is 111?
            if (bit3) {

                // if the fourth bit is true - the binary nibble is 1111
                if (bit4) {

                    // return the 'F' hexidecimal character
                    return HexadecimalConstants.F;

                    // else the fourth bit is false - the binary nibble is 1110
                } else {

                    // return the 'E' hexidecimal character
                    return HexadecimalConstants.E;

                }

                // else the third bit is false - the binary nibble is 110?
            } else {

                // if the fourth bit is true - the binary nibble is 1101
                if (bit4) {

                    // return the 'D' hexidecimal character
                    return HexadecimalConstants.D;

                    // else the fourth bit is false - the binary nibble is 1100
                } else {

                    // return the 'C' hexidecimal character
                    return HexadecimalConstants.C;

                }

            }

/* ... Snipped 100+ lines ... */

                // else the third bit is false - the binary nibble is 000?
            } else {

                // if the fourth bit is true - the binary nibble is 0001
                if (bit4) {

                    // return the '1' hexidecimal character
                    return HexadecimalConstants.ONE;

                    // else the fourth bit is false - the binary nibble is 0000
                } else {

                    // return the '0' hexidecimal character
                    return HexadecimalConstants.ZERO;

                }

            }

        }

    }

}
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!