如何在Progress 4GL中创建十六进制转二进制的计算器

huangapple go评论65阅读模式
英文:

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

问题

我正在学习Progress 4GL。

在Progress 4GL中有没有办法制作一个可以将十六进制转换为二进制以及反向转换的计算器?

我只需要一个可以将十六进制转换为二进制以及反向转换的计算器。谢谢。

英文:

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

答案1

得分: 1

如果您正在运行一个相对现代的OpenEdge版本,您可以使用内置的MathUtil的IntToHexHexToInt方法。请参阅https://documentation.progress.com/output/oehttpclient/oe127/OpenEdge.Core.Util.MathUtil.html和https://github.com/progress/ade/blob/release-12.7.x/corelib/OpenEdge/Core/Util/MathUtil.cls

英文:

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

答案2

得分: 0

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.
英文:

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.

huangapple
  • 本文由 发表于 2023年6月26日 11:27:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定