在将comp3位数字解压缩到从旧版系统(主机)提取的文件中时遇到问题。我尝试了由同一个堆栈溢出成员@Jose Ventura给出的代码,但是没有用。我尝试打印解压后的数据,但没有打印任何内容。我对这个概念完全陌生(比较3)。你们可以帮我完成这项任务
下面是我的代码
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
public class TestComp3 {
private static String unpackData(byte[] packedData) {
int decimals = 2;
String unpackedData = "";
final int negativeSign = 13;
int lengthPack = packedData.length;
int numDigits = lengthPack * 2 - 1;
int raw = (packedData[lengthPack - 1] & 0xFF);
int firstDigit = (raw >> 4);
int secondDigit = (packedData[lengthPack - 1] & 0x0F);
boolean negative = (secondDigit == negativeSign);
int lastDigit = firstDigit;
for (int i = 0; i < lengthPack - 1; i++) {
raw = (packedData[i] & 0xFF);
firstDigit = (raw >> 4);
secondDigit = (packedData[i] & 0x0F);
unpackedData += String.valueOf(firstDigit);
unpackedData += String.valueOf(secondDigit);
}
unpackedData += String.valueOf(lastDigit);
if (decimals > 0) {
unpackedData = unpackedData.substring(0, numDigits - decimals) + "."
+ unpackedData.substring(numDigits - decimals);
}
if (negative) {
// System.out.println("unpacked data :-"+unpackedData);
System.out.println("-" + unpackedData);
return '-' + unpackedData;
}
System.out.println(unpackedData);
return unpackedData;
}
public static void main(String[] args) {
Path path = Paths.get("C:\\Users\\AV00499269\\Desktop\\Comp3 data file\\KYC.txt");
try {
byte[] data = Files.readAllBytes(path);
TestComp3.unpackData(data);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
答案 0 :(得分:0)
您的问题是,您尝试获取未压缩半字节的字符串值,而这些半字节将是不可打印的控制字符。
对于每个字节,将打包的数字作为字节数组处理:
int endValue = 0;
for (int I = 0, I< packed.length; I++) {
byte high = packed[I] & 0xF0;
byte low = packed[I] & 0x0F;
endValue = (endValue * 10) + (high * 10) + low;
}
System.out.println("The int value is: " + endValue)
根据源系统的不同,您需要对符号小节进行特殊处理,如果为负,则乘以-1,但是我将其留给读者作为特殊练习。