How to create calculator for hexadecimal to binary in Progress 4GL

78 Views Asked by At

I'm learning Progress 4GL.

Is there anyway to make a calculator that can convert Hexadecimal to binary and the otherway around in Progress 4GL?

I just need a calculator that can convert Hexadecimal to binary and the otherway around. Thanks

2

There are 2 best solutions below

0
TheDrooper On

Converting back and forth between binary and hexadecimal is fairly straightforward. You just need to convert groups of four binary numbers to their hex equivalent, and hex digits to their binary equivalent. These two procedures will do that.

PROCEDURE binToHex:
    DEFINE INPUT PARAMETER pcBin AS CHARACTER NO-UNDO.
    DEFINE OUTPUT PARAMETER pcHex AS CHARACTER NO-UNDO.

    DEFINE VARIABLE iFill AS INTEGER NO-UNDO.
    DEFINE VARIABLE cBinGroup AS CHARACTER NO-UNDO.
    DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.

    /* Pad the left side with zeroes to make the string evenly divisible into 4-bit groups */
    iFill = LENGTH(pcBin) MOD 4.
    IF iFill <> 0 THEN
        pcBin = FILL("0", 4 - iFill) + pcBin.

    /* Convert each group of 4 bits to a hex digit */
    DO iLoop = 1 TO LENGTH(pcBin) BY 4:
        cBinGroup = SUBSTRING(pcBin, iLoop, 4).
                
        CASE cBinGroup:
            WHEN "0000" THEN pcHex = pcHex + "0".
            WHEN "0001" THEN pcHex = pcHex + "1".
            WHEN "0010" THEN pcHex = pcHex + "2".
            WHEN "0011" THEN pcHex = pcHex + "3".
            WHEN "0100" THEN pcHex = pcHex + "4".
            WHEN "0101" THEN pcHex = pcHex + "5".
            WHEN "0110" THEN pcHex = pcHex + "6".
            WHEN "0111" THEN pcHex = pcHex + "7".
            WHEN "1000" THEN pcHex = pcHex + "8".
            WHEN "1001" THEN pcHex = pcHex + "9".
            WHEN "1010" THEN pcHex = pcHex + "A".
            WHEN "1011" THEN pcHex = pcHex + "B".
            WHEN "1100" THEN pcHex = pcHex + "C".
            WHEN "1101" THEN pcHex = pcHex + "D".
            WHEN "1110" THEN pcHex = pcHex + "E".
            WHEN "1111" THEN pcHex = pcHex + "F".
            OTHERWISE /* If it's not valid binary digits, return an error */
            DO:
                pcHex = "Error".
                RETURN.
            END.
        END CASE.
    END.  /* DO iLoop */

END PROCEDURE.

PROCEDURE hexToBin:
    DEFINE INPUT PARAMETER pcHex AS CHARACTER NO-UNDO.
    DEFINE OUTPUT PARAMETER pcBin AS CHARACTER NO-UNDO.

    DEFINE VARIABLE iLoop AS INTEGER NO-UNDO.

    /* Loop through hex input, converting each digit to binary */
    DO iLoop = 1 TO LENGTH(pcHex):
        CASE SUBSTRING(pcHex, iLoop, 1):
            WHEN "0" THEN pcBin = pcBin + "0000".
            WHEN "1" THEN pcBin = pcBin + "0001".
            WHEN "2" THEN pcBin = pcBin + "0010".
            WHEN "3" THEN pcBin = pcBin + "0011".
            WHEN "4" THEN pcBin = pcBin + "0100".
            WHEN "5" THEN pcBin = pcBin + "0101".
            WHEN "6" THEN pcBin = pcBin + "0110".
            WHEN "7" THEN pcBin = pcBin + "0111".
            WHEN "8" THEN pcBin = pcBin + "1000".
            WHEN "9" THEN pcBin = pcBin + "1001".
            WHEN "A" THEN pcBin = pcBin + "1010".
            WHEN "B" THEN pcBin = pcBin + "1011".
            WHEN "C" THEN pcBin = pcBin + "1100".
            WHEN "D" THEN pcBin = pcBin + "1101".
            WHEN "E" THEN pcBin = pcBin + "1110".
            WHEN "F" THEN pcBin = pcBin + "1111".
            OTHERWISE /* If it's not a valid hex digit, return an error */
            DO:
                pcBin = "Error".
                RETURN.
            END.
        END CASE.
    END.  /* DO iLoop */

END PROCEDURE.


DEFINE VARIABLE pcOutput AS CHARACTER NO-UNDO.

RUN binToHex (INPUT "1111101011011", OUTPUT pcOutput).
MESSAGE pcOutput VIEW-AS ALERT-BOX INFORMATION.

RUN hexToBin (INPUT "1F5B", OUTPUT pcOutput).
MESSAGE pcOutput VIEW-AS ALERT-BOX INFORMATION.
0
nwahmaet On

If you are running a reasonably modern version of OpenEdge, you can use the shipped MathUtil's IntToHex and HexToInt methods. See https://documentation.progress.com/output/oehttpclient/oe127/OpenEdge.Core.Util.MathUtil.html and https://github.com/progress/ade/blob/release-12.7.x/corelib/OpenEdge/Core/Util/MathUtil.cls