如何将解包十进制转换回 COMP-3?

发布于 2024-10-06 01:48:35 字数 1431 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黯然 2024-10-13 01:48:35

在压缩十进制中 -123 表示为 X'123d'(最后一个字节 c、d 或 f 是符号)。
处理压缩十进制最简单的方法之一就是简单地转换字节
转换为十六进制字符串(或根据需要反之亦然),然后使用正常的字符串操作。这可能不是最有效的,但很容易实现。

因此,将整数(值)转换为压缩十进制大致如下(注意:我尚未测试代码)

String sign = "c";
if (value < 0) {
    sign = "d";
    value = -1 * value;
}
String val = value + "d"

byte[] comp3Bytes = new BigInteger(val, 16).toByteArray();

以下是一些用于转换为 comp3 或从 comp3 转换的示例代码
要从字节数组中检索压缩十进制,请参阅方法 getMainframePackedDecimal
http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Common/Conversion.java?revision=3& view=markup

并设置压缩十进制,请参阅

两个例程都采用字节数组、起始位置和字段位置的长度。

网上还有其他这样做的示例(我认为 JRanch 也有用于执行转换的代码),请进行一些谷歌搜索。

In packed decimal -123 is represented as X'123d' (the last nyble c,d or f being the sign).
One of the simplest ways to handle packed decimal is to simply convert the bytes
to a hex string (or vice versa as required) then use normal string manipulation. This may not be the most efficient but it is easy to implement.

So to convert a Integer (value) to packed decimal is roughly (note: I have not tested the code)

String sign = "c";
if (value < 0) {
    sign = "d";
    value = -1 * value;
}
String val = value + "d"

byte[] comp3Bytes = new BigInteger(val, 16).toByteArray();

Following are some example code for converting to/from comp3
To retrieve a packed decimal from an array of bytes see method getMainframePackedDecimal
in http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Common/Conversion.java?revision=3&view=markup

and to set a packed decimal see
setField in http://record-editor.svn.sourceforge.net/viewvc/record-editor/Source/JRecord/src/net/sf/JRecord/Types/TypePackedDecimal.java?revision=3&view=markup

both routines take an array of bytes, a start position and either a length of a field position.

There are other examples of doing this on the web (JRanch I think has code for doing the conversion as well), do a bit of googling.

中二柚 2024-10-13 01:48:35

将分区十进制转换为 comp-3 非常简单 - 翻转低字节的半字节并去除所有其他字节的高半字节。

考虑数字 12345——用压缩十进制表示法来说,这将是 x'12345C' 或 x'12345F'(C 和 F 都是 +,AB 和 D 都是 -)。当您将其转换为分区十进制时,您翻转了低半字节并在每个数字之间的高半字节中插入了“F”。将其变成 x'F1F2F3F4C5'。

要将其转换回来,只需颠倒该过程即可。使用 java,看起来像:

byte[] myDecimal = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5 };
byte[] myPacked = new byte[3];

//Even low nibble moved to high nibble and merged with odd low nibble
myPacked[0] = ((myDecimal[0] & 0b00001111)  << 4)) | (myDecimal[1] & 0b00001111);
myPacked[1] = ((myDecimal[2] & 0b00001111)  << 4)) | (myDecimal[3] & 0b00001111);

//Last byte gets filpped for sign
myPacked[2] = ((myDecimal[5] & 0b00001111)  << 4)) | (myDecimal[4] & 0b00001111);

Converting zoned decimal to comp-3 is quite easy -- flip the nibbles of the low byte and strip off the high nibble of all other bytes.

Consider the number 12345 -- in packed decimal notation, that would be a x'12345C' or x'12345F' (both C and F are +, A B and D are -). When you converted it to zoned decimal, you flipped the low nibble and inserted a "F" in the high nibble between each digit. Turning it into x'F1F2F3F4C5'.

To convert it back, you just reverse the process. Using java, that would look like:

byte[] myDecimal = { 0xF1, 0xF2, 0xF3, 0xF4, 0xF5 };
byte[] myPacked = new byte[3];

//Even low nibble moved to high nibble and merged with odd low nibble
myPacked[0] = ((myDecimal[0] & 0b00001111)  << 4)) | (myDecimal[1] & 0b00001111);
myPacked[1] = ((myDecimal[2] & 0b00001111)  << 4)) | (myDecimal[3] & 0b00001111);

//Last byte gets filpped for sign
myPacked[2] = ((myDecimal[5] & 0b00001111)  << 4)) | (myDecimal[4] & 0b00001111);
最终幸福 2024-10-13 01:48:35

当我过去用 Java 搞乱 COMP-3 时,我最终编写了一个方法来读取字节并将它们转换为数字。我不认为我必须写出 COMP-3,但我想我会反过来使用相同的逻辑。

When I have messed with COMP-3 in the past with Java I ended up writing a method to read in the bytes and convert them to a number. I don't think I ever had to write COMP-3 out, but I assume I would use the same logic in reverse.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文