This works fine for me. Try it out ;) I just replaced the output stream for testing purposes, reorganized the code and added a "0" at the beginning of the String if it has an odd length.
public void StringtoBCD(String StringElement) {
PrintStream in = System.out;
if(StringElement.length()%2 == 1) {
StringElement= "0"+StringElement;
}
if (!" ".equals(StringElement)){
byte[] tempBCD = StringElement.getBytes();
for (int i=0; i<tempBCD.length; i++){
tempBCD[i]=(byte)(tempBCD[i]-0x30);
}
for(int i = 0; i<tempBCD.length; i=i+2){
in.write((tempBCD[i]<<4)+tempBCD[i+1]);
}
}
in.flush();
}
Btw. this does no work if StringElement contains A to F.