Menu

#2 Code returns CharIDs rather than the actual text w/JDK 1.2

open
nobody
None
5
2000-09-13
2000-09-13
No

When run under JDK 1.2, the software returns something like this instead of the desired results:

Code: [C@fc38135c[C@fc38135c[C@fc38135c[C@fc38135c[C@fc38135c[C@fc38135c [C@fc38135c [C@fc38135c[C@fc38135c[C@fc38135c[C@fc38135c

This is due to the way that you were creating the result string. Below is a version of the software that will return the expected results under JDK 1.2. Basically, it uses a StringBuffer, which is designed to be able to concatenate chars in a easy format. For the same barcode swipe as the above, the following is returned:

Code: 000000000111242002 UPA 043100750963

Here's the updated decode function:

public String decode(String inVal){
inVal = inVal.substring(1,inVal.length()-1); //strip off leading and following periods
StringBuffer retVal = new StringBuffer();
byte c = 0;
int i = 0;
int n= 0;
int count = 0;
char decoded_triplet[] = new char[3];
char decodedChar;
for (i = 0;i<inVal.length();i++){
c = decodeChar(inVal.charAt(i));
if (c < 0){
//System.err.println("error skipping this char");
retVal.append(' ');
}
else{
c = (byte)(63 & c);
n = n << 6 | c;
count++;
if (count == 4){
decoded_triplet[0] = (char)((n >> 16) ^ 67);
decoded_triplet[1] = (char)((n >> 8 & 255) ^ 67);
decoded_triplet[2] = (char)((n & 255) ^ 67);
retVal.append(decoded_triplet);
n = 0;
count = 0;
}
}

}
if (count == 2 ){ //ie if it ended w/ an odd set of data...
n = n << 12;
retVal.append((char)((n >> 16) ^ 67));
}
if (count == 3){
n = n << 6;
retVal.append((char)((n >> 16) ^ 67));
retVal.append((char)((n >> 8 & 255) ^ 67));
}
String realResult = new String(retVal);
return realResult;
}

Discussion


Log in to post a comment.