如何在 COBOL 中对 X pic 子句执行算术
我想在 COBOL 中编写一个函数,将字符从 PIC X(60) 数据类型转换为大写。虽然我知道一种方法,但我希望能够手动浏览字母并添加一个常量以从小写变为大写,但我似乎无法对 PIC X 数据类型执行任何算术,除非我将它们重新定义为其他东西,但随后我似乎破坏了原始的 X 字符。
有人可以给我一个例子,说明如何在给定 PIC X 变量的情况下将其增加 1 个字母(即 a -> b、d -> e...等)。换句话说,我希望能够为这个变量加 1:
01 char-temp pic x.
谢谢,
Ivan
I would like to write a function in COBOL that converts characters from a PIC X(60) data type to uppercase. While I know one way of doing this, I would like to be able to manually go through the letters and add a constant to go from lower case to upper case, but I cannot seem to be able to perform any arithmetic on PIC X data types, unless I redefine them as something else, but then I seem to corrupt the original X character.
Can someone give me an example of how, given a PIC X variable, you can increment it by 1 letter (i.e. a -> b, d -> e... etc). In other words, I would like to be able to add 1 to this variable:
01 char-temp pic x.
Thanks,
Ivan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以这种方式进行大小写折叠通常是一个非常糟糕的主意,它实际上只适用于 7 位 ASCII。你问的是可能的,甚至很容易做到,但我会首先提到一些其他方法来大写你的数据。
最简单的方法:
非常简单的方法:
困难的方法,将 1 添加到 01 char-temp pic x:
。
然后你可以:
并且 char-temp 将包含一个“B”。正如其他人提到的,Cobol 对于单字节二进制数学不太好,因此您必须使字符适合二进制数的低字节,在本例中是 2 字节二进制数。
It is usually a very bad idea to do case folding this way, it really only works for 7-bit ASCII. What you ask is possible, and even easy to do, but I'll mention some other ways to uppercase your data first.
Easiest way:
Very easy way:
Hard way, to add one to 01 char-temp pic x:
.
Then you can:
And char-temp will contain a 'B'. As others have mentioned, Cobol is not nice about single byte binary math, so you have to make your character fit in the low byte of a binary number, in this case a 2 byte binary number.
你可以顽皮地使用重新定义,例如:
或使用引用修改:
我相信它们也是其他方法......
You could be naughty and use redefines eg:
Or use reference modification:
and I am sure they are other approaches too...
单个字符通常占用一个字节的内存。标准 COBOL 没有单字节的二进制数值数据类型(注意:
PIC 9 USAGE DISPLAY
是数字的字符表示形式,而不是数字的二进制表示形式)。因此,在 COBOL 中对单个字符进行二进制算术对您来说不起作用。即使您可以对字符执行算术运算,您也可能对字符的二进制表示形式做出错误的假设 - 字母“b”不一定等于“a”加 1 的二进制表示形式(它可能适用于 ASCII,但不适用于 ASCII)对于 EBCDIC)。
最后,如果您想用另一个字符替换一个字符,您应该研究 COBOL
INSPECT
语句。与位调整不同,INSPECT
是完全可移植的。A single character generally takes one byte of memory. Standard COBOL does not have a binary numeric data type of one byte (Note:
PIC 9 USAGE DISPLAY
is a character representation of a digit not the binary representation of the digit). So, doing binary arithemetic on single characters in COBOL isn't going to work for you.Even if you could perform arithementic on characters, you may be making a bad assumption about the binary representation of characters - the letter 'b' is not necessarily equal to the binary representation of 'a' plus 1 (it might work for ASCII but not for EBCDIC).
Finally, if you want to replace one character by another you should investigate the COBOL
INSPECT
statement. Unlike bit twiddling,INSPECT
is completely portable.对于 COBOL 85,可以使用内部函数
CHAR
和ORD
。结果:
For COBOL 85, intrinsic functions
CHAR
andORD
may be used.Result: