How do you convert from Hex to Ascii using different inputs in COBOL

时光毁灭记忆、已成空白 提交于 2021-01-29 07:41:48

问题


My intention is to be able to print ascii from HEX-NUM, but I want to be able to change the input Hex values, sort of like this:

WORKING-STORAGE SECTION.
01  HEX-INPUT PIC X(2) VALUE "3C".
01  HEX-NUM    PIC X VALUE X"HEX-INPUT". 

However this leads to an error because the X"..." is trying to read HEX-INPUT as Hex values whereas I want to access the value I have defined as 3C.

Any ideas what I need to do to achieve this flexibility I desire? I need to change HEX-INPUT often in my program hence the conundrum.


回答1:


Remove HEX-INPUT and use.

01  HEX-NUM PIC X VALUE X"3C".

If you had in mind converting two hex characters to a character value, this code should be fine.

   WORKING-STORAGE SECTION.
   01  HEX-INPUT PIC XX VALUE "3C".
   01  HEX-NUM PIC X.
   01  HEX-TABLE PIC X(16) VALUE "0123456789ABCDEF".
   01  HEX-VALUE COMP PIC 9(4).
   01  HEX-TALLY COMP PIC 9(4).
   PROCEDURE DIVISION.
   BEGIN.
       MOVE 0 TO HEX-TALLY
       INSPECT HEX-TABLE TALLYING HEX-TALLY FOR
           CHARACTERS BEFORE HEX-INPUT (1:1)
       COMPUTE HEX-VALUE = HEX-TALLY * 16
       MOVE 0 TO HEX-TALLY
       INSPECT HEX-TABLE TALLYING HEX-TALLY FOR
           CHARACTERS BEFORE HEX-INPUT (2:1)
       ADD HEX-TALLY TO HEX-VALUE
       MOVE FUNCTION CHAR (HEX-VALUE + 1) TO HEX-NUM
       STOP RUN
       .

The two characters 3C are converted to a single character with a value of X"3C".


There is an alternative to the code, above. I did not post it earlier because ordinal values and alphabets can be confusing; but, since you raised the issue in a comment, ....

In the following, hex2val and val2hex use a limited alphabet containing only the hexadecimal characters. Because they restrict the alphabet, they must called as separately compiled programs and not copied into any other program, such as performed procedures or nested programs.

hex2val converts a two character hexadecimal data item to its equivalent numeric value.

val2hex converts a numeric value to its equivalent two character hexadecimal data item.

Note: the use of - 1 and + 1 with the ORD and CHAR functions, respectively. Required because "0" is the first character (CHAR (1)) in the limited alphabet.


   program-id. hex2val.
   environment division.
   configuration section.
   object-computer. computer-name-here
       sequence hex-digits
       .
   special-names.
       alphabet hex-digits
         "0" thru "9" "A" thru "F"
       .
   data division.
   linkage section.
   01 hex-in pic xx.
   01 hex-value comp pic 9(4).
   procedure division using hex-in hex-value.
   begin.
       compute hex-value =
           (function ord (hex-in (1:1)) - 1) * 16
         + (function ord (hex-in (2:1)) - 1)
       exit program
       .
   end program hex2val.

   program-id. val2hex.
   environment division.
   configuration section.
   object-computer. computer-name-here
       sequence hex-digits
       .
   special-names.
       alphabet hex-digits
         "0" thru "9" "A" thru "F"
       .
   data division.
   linkage section.
   01 hex-value comp pic 9(4).
   01 hex-out pic xx.
   procedure division using hex-value hex-out.
   begin.
       move function char ( hex-value / 16 + 1 )
           to hex-out (1:1)
       move function char (
           ( function mod (hex-value 16)) + 1 )
           to hex-out (2:1)
       exit program
       .
   end program val2hex.

Note that some names were changed, but the earlier program can be reduced to a CALL and a MOVE. Additional code shows calling the companion program.

   data division.
   working-storage section.
   1 hex-in pic xx value "3C".
   1 hex-out pic xx value "  ".
   1 hex-char pic x.
   1 hex-value comp pic 9(4).
   procedure division.
   begin.
       call "hex2val" using hex-in hex-value
       move function char (hex-value + 1) to hex-char
       call "val2hex" using hex-value hex-out
       display hex-out
       stop run
       .


来源:https://stackoverflow.com/questions/63526832/how-do-you-convert-from-hex-to-ascii-using-different-inputs-in-cobol

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!