skip to Main Content

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.

2

Answers


  1. If you use ISO/IEC 8859-1 for encoding, it appears to preserve the -16 byte.

    byte[] bytes = { 0x1b, (byte) 0xf0, 0x06, 0x01, 0x01 };
    
    String stringA = new String(bytes, StandardCharsets.ISO_8859_1);
    String stringB = new String(bytes, StandardCharsets.UTF_8);
    String stringC = new String(bytes, StandardCharsets.US_ASCII);
    
    System.out.println(Arrays.toString(stringA.getBytes(StandardCharsets.ISO_8859_1)));
    System.out.println(Arrays.toString(stringB.getBytes(StandardCharsets.UTF_8)));
    System.out.println(Arrays.toString(stringC.getBytes(StandardCharsets.US_ASCII)));
    

    Output

    [27, -16, 6, 1, 1]
    [27, -17, -65, -67, 6, 1, 1]
    [27, 63, 6, 1, 1]
    
    Login or Signup to reply.
  2. When converting between strings and byte arrays, you are using some encoding mechanism. If you don’t specify it, it will use the default encoding for your platform. In your case, it seems to be UTF-8 because some characters are encoded on 3 bytes.
    With the ISO-8859-1 encoding, each character is encoded as a single byte. Try using it instead:

    ...
    import static java.nio.charset.StandardCharsets.ISO_8859_1;
    ...
    
        final byte[] bytes = {0x1B, (byte) 0xF0 , 0x06, 0x01, 0x01};
        String newString = new String(bytes, ISO_8859_1);
    
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();
    
        for (byte b : newString.getBytes(ISO_8859_1)) {
            System.out.print(b + " ");
        }
        System.out.println();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search