如何在COBOL中使用不同的输入将十六进制转换为Ascii [英] How do you convert from Hex to Ascii using different inputs in COBOL

查看:237
本文介绍了如何在COBOL中使用不同的输入将十六进制转换为Ascii的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意图是能够从HEX-NUM打印ascii,但是我希望能够更改输入的十六进制值,就像这样:

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". 

但是这会导致出现错误,因为 X"..." 试图将HEX-INPUT读取为十六进制值,而我想访问该值I定义为3C.

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.

推荐答案

删除 HEX-INPUT 并使用.

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
       .

将两个字符 3C 转换为值为X"3C"的单个字符.

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, ....

在下面, hex2val val2hex 使用仅包含十六进制字符的有限字母.因为它们限制了字母,所以它们必须作为单独编译的程序来调用,并且不能复制到任何其他程序中,例如已执行的过程或嵌套的程序.

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 将两个字符的十六进制数据项转换为其等效的数值.

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

val2hex 将数字值转换为其等效的两个字符的十六进制数据项.

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

注意:-1 +1 分别与 ORD CHAR 函数一起使用.必需,因为"0" 是受限字母中的第一个字符( CHAR(1)).

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.


请注意,某些名称已更改,但是较早的程序可以简化为 CALL MOVE .附加代码显示了调用配套程序.


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
       .

这篇关于如何在COBOL中使用不同的输入将十六进制转换为Ascii的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆