I have a problem while unpacking the comp3 digits in a file which is extracted from the legacy system(main frame). I tried the code which was given by a fellow stack overflow member @Jose Ventura but it didnt work. I tried to print the unpacked data but nothing got printed. I am totally new to this concept(comp 3). So can you guys help me to achieve this task
Below is my code
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();
}
}
}
Your problem is that you are trying to take the string value of the unpacked nibbles, and those will be non-printable control characters.
Treat your packed number as a byte array, for each byte:
Depending on the source system, you will need to special case the sign nibble, and multiply by -1 if it is negative, but I'll leave that as a special exercise for the reader.