I am using a ThermalPrinter for recipes in java, or i try to. This printer accepts only Text for printing, so i need to send all commands as strings. Most are just some sequence of the asci commands. So i collect all the text i want to print and all commands that need to be done in one String and then send this to the printer. I add the commands to my string like this:
final byte[] bytes = {0x1B, 0x25, 0x18};
printString += new String(bytes);
This works fine except for on case. In the final Command to cut the paper on of the values is 0xF0. this works out to -16 since java interprets byte as signed value. If then add this to the string it adds not the bytes i want. It adds 3 bytes instead which will be for the -16 as text. But it should just add the one byte.
If i run this:
final byte[] bytes = {0x1B, (byte) 0xF0 , 0x06, 0x01, 0x01};
String newString = new String(bytes);
for (byte b : bytes) {
System.out.print(b + " ");
}
System.out.println();
for (byte b : newString.getBytes()) {
System.out.print(b + " ");
}
System.out.println();
It gives me:
27 -16 6 1 1
27 -17 -65 -67 6 1 1
The byte array contains the correct value but not the string. I would expect the two outputs to be the same. Is this a limitation of the String class? Or is there a way to do this?
Some more information. The printer is a Hengstler eXtendo X-56. To communicate to the printer i use the API supplied by the Manufacturer. Its a C lib i add to my system(debian) and then load via JNA. The Method from api header i use to print data.
int exo_api_printer_write(intptr_t printer, unsigned char * data, int size, unsigned long timeout_ms)
The JNA interface funtion looks like:
int exo_api_printer_write(Pointer printer, String data, int size, long timeout_ms);
So at some point i Have to convert my bytes into a String to send it to the printer. My Problem now is the printer command to cute the paper. The command is: 0x1B, 0xF0, 0x06, 0x01, 0x01 and cannot convert this to a string.