java - 无法在java中将comp 3解压缩为数字
问题描述
我在互联网上引用了一个代码来将 comp 3 解压缩为 java 中的数字。我试图将示例 comp3 文件传递给代码,但没有得到正确的解包数据。我得到了一些奇怪的数字。我是这个概念的新手(comp 3),所以你们能帮我解决这个问题。提前致谢
下面是我的代码
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Converts between integer and an array of bytes in IBM mainframe packed
* decimal format. The number of bytes required to store an integer is (digits +
* 1) / 2. For example, a 7 digit number can be stored in 4 bytes. Each pair of
* digits is packed into the two nibbles of one byte. The last nibble contains
* the sign, 0F for positive and 0C for negative. For example 7654321 becomes
* 0x76 0x54 0x32 0x1F.
*
* This class is immutable. Once constructed you can extract the value as an
* int, an array of bytes but you cannot change the value. Someone should
* implement equals() and hashcode() to make this thing truly useful.
*/
public class PackedDecimalToComp {
public static void main(String[] args) {
try {
// test.unpackData(" 0x12345s");
Path path = Paths.get("C:\\Users\\AV00499269\\Desktop\\Comp3 data file\\Comp3Test.txt");
byte[] data = Files.readAllBytes(path);
PackedDecimalToComp test = new PackedDecimalToComp();
test.unpackData(data);
} catch (Exception ex) {
System.out.println("Exception is :" + ex.getMessage());
}
}
private static String unpackData(byte[] packedData) {
String unpackedData = "";
final int negativeSign = 13;
for (int currentCharIndex = 0; currentCharIndex < packedData.length; currentCharIndex++) {
byte firstDigit = (byte) ((packedData[currentCharIndex] >>> 4) & 0x0F);
byte secondDigit = (byte) (packedData[currentCharIndex] & 0x0F);
unpackedData += String.valueOf(firstDigit);
if (currentCharIndex == (packedData.length - 1)) {
if (secondDigit == negativeSign) {
unpackedData = "-" + unpackedData;
}
} else {
unpackedData += String.valueOf(secondDigit);
}
}
System.out.println("Unpackeddata is :" + unpackedData);
return unpackedData;
}
}
我传递的 Comp3 文件有值x019F
转换后,我得到了解压缩的数据783031394
解决方案
您可以使用免费工具IBM Record Generator for Java。
这允许您生成一个表示 COBOL 或 PL/I DSECT 的 Java 类,然后您可以在自己的代码中使用它来读取/写入大多数 COBOL 和 PL/I 数据类型的值。如果您不使用结构,那么您可以通过代码了解如何使用底层JZOS类与数据类型进行交互。
尽管该工具是免费的,但它受到 IBM 的支持,因此如果您遇到问题,您可以向 IBM 提出问题,他们会修复它。
推荐阅读
- python - GofChisquarePower solve_power
- angular - Angular 材质 matNoDataRow 在 Angular 11 中不起作用
- c++ - С++ 抽象返回类型
- mongodb - 使用 UUID v4 的键集分页
- asp.net - ASP.NET 中的更新导致异常:“必须声明标量变量”
- python-3.x - 使用 n 个唯一整数生成不带替换的随机整数以获得固定总和(Python)
- arrays - 将数组添加到 Angular 中的数组
- python - 如何从 3D 制作 2D ndarray 以使 (100, 50, 20) 为 (100, 100)
- html - CSS图标故障?
- optaplanner - 光规划器。学校时间表。强制第一节课