gpt4 book ai didi

ascii - 如何使用 Java 解压 COMP-3 数字?

转载 作者:行者123 更新时间:2023-12-02 04:40:13 26 4
gpt4 key购买 nike

我有一个巨大的主机文件,并且该文件中有一些压缩的数字。我想知道如何使用java解压以下数字?

压缩数字:?

我阅读了解包数字的教程,发现了以下规则来计算解包数字所需的字节数:

total_number_of_bytes =(位数 + 1)/2

我编写了以下代码来解压数字:

public String unpackData(String packedData, int decimalPointLocation) {
String unpackedData = "";
char[] characters = packedData.toCharArray();
final int impliedPositive = 15;
final int positiveNumber = 12;
final int negativeNumber = 13;
for (int currentCharIndex = 0; currentCharIndex < characters.length; currentCharIndex++) {
byte[] unpackedDigits = unpackByte((byte) characters[currentCharIndex]);
if(currentCharIndex == (characters.length - 1)) {
if(unpackedDigits[1] == impliedPositive || unpackedDigits[1] == positiveNumber) {
unpackedData += String.valueOf(unpackedDigits[0]);
} else if(unpackedDigits[1] == negativeNumber) {
unpackedData = "-" + unpackedData;
}
} else {
unpackedData += String.valueOf(unpackedDigits[0]) + String.valueOf(unpackedDigits[1]);
}
}
if(decimalPointLocation > 0) {
unpackedData = unpackedData.substring(0, (decimalPointLocation - 1)) +
"." +
unpackedData.substring(decimalPointLocation);
}
return unpackedData;
}

private byte[] unpackByte(byte packedData) {
byte firstDigit = (byte) (packedData >>> 4);
firstDigit = setBitsToZero(firstDigit, 4, 8);

//System.out.println(" firstDigit = "+ firstDigit + ", and its bit string after unpacking = " + getBitString(firstDigit, 7));

byte secondDigit = setBitsToZero(packedData, 4, 8);
//System.out.println("second digit = " + secondDigit + ", and its bit string of second digit after unpcking = " + getBitString(secondDigit, 7));

byte[] unpackedData = new byte[2];
unpackedData[0] = firstDigit;
unpackedData[1] = secondDigit;
return unpackedData;
}

private byte setBitsToZero(byte number, int startBitPosition, int endBitPosition) {
for (int i = startBitPosition; i < endBitPosition; i++) {
number = (byte) (number & ~(1 << i));
}
return number;
}

该程序对于整数类型值可以正常工作,但对于浮点类型值则不起作用。

谁能告诉我我的程序是否正确?

最佳答案

COMP-3(或“压缩十进制”)数据如下所示:0x12345s,其中“s”是 C(正数)、D(负数)或 F(无符号数)。因此 0x12345c -> "12345"、x012345d -> "-12345"和 0x12345f -> "12345"。

您遇到了一个明显的错误:如果符号为负,您将忽略包含符号 nybble 的字节中的 nybble(例如,上面的“5”)。此外,您在操作半字节方面付出了太多的努力,这是一个简单的按位与或 4 位移位来隔离半字节。

尝试这样的事情(未经测试):

public String unpackData(String packedData, int decimalPointLocation) {
String unpackedData = "";
char[] characters = packedData.toCharArray();
final int negativeSign = 13;
for (int currentCharIndex = 0; currentCharIndex < characters.length; currentCharIndex++) {
byte firstDigit = ((byte) characters[currentCharIndex]) >>> 4);
byte secondDigit = ((byte) characters[currentCharIndex]) & 0x0F;
unpackedData += String.valueOf(firstDigit);
if (currentCharIndex == (characters.length - 1)) {
if (secondDigit == negativeSign) {
unpackedData = "-" + unpackedData;
}
} else {
unpackedData += String.valueOf(secondDigit);
}
}
if (decimalPointLocation > 0) {
unpackedData = unpackedData.substring(0, (decimalPointLocation - 1)) +
"." +
unpackedData.substring(decimalPointLocation);
}
return unpackedData;
}

关于ascii - 如何使用 Java 解压 COMP-3 数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56522518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com