我正在学习 Progress 4GL。
有没有办法制作一个可以在 Progress 4GL 中将十六进制转换为二进制以及相反的计算器?
我只需要一个可以将十六进制转换为二进制以及反之的计算器。谢谢
在二进制和十六进制之间来回转换相当简单。您只需将四个二进制数组成的组转换为相应的十六进制数字,并将十六进制数字转换为相应的二进制数字。这两个程序就可以做到这一点。
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.
如果您运行的是相当现代的 OpenEdge 版本,则可以使用附带的 MathUtil 的
IntToHex
和 HexToInt
方法。请参阅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