I have a cobol file which is not in human-readable format: it has data(numbers) in comp-3 format, but also other strings and characters. I have the algorithm for converting the comp-3 format, but when I apply it to the array of bytes from the file, all the characters get converted and the output is not the right one. How could I decode the entire file correctly, taking into consideration both comp-3 data and normal data?
I will add some lines from the file below and also my algorithm for comp-3:
The file in Notepad++ looks like this(first lines):
AH4820øêæÉ* 200 DBAG DBAG 0
AED S EUR AED S KAS°ê¤ 2 ø TN Øê¤ ð §É! SN ê¤
The file in Notepad++ with the ASCII to HEX converted looks like this, even though this should not be right:
200F41483438323002C3B8C3AA01C3A6 01C3892A202020202020202020202020 20203230302044424147204442414720 30202020202020202020202020202020
public 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);
}
}
return unpackedData;
}