我有巨大的大型机文件,并且该文件中有一些压缩数字。我想知道如何使用java解压后面的数字?
压缩数字:?
我阅读了解包数字的教程,发现以下规则来计算解包数字所需的字节数:
total_number_of_bytes = (no. of digits + 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;
}
该程序适用于整数类型值,但不适用于浮点类型值。
谁能告诉我的程序是否正确?