5

I have an Android app and I'm trying to print some texts with it that contain non-latin characters.

I'm using this code to send ESC t n command to the printer:

 byte[] buf = new byte[]{0x1B, 0x74, (byte)2}; // 2 is the codetable for PC850: Multilingual
 this.mBaseOutputStream.write(buf);

Then, I try to print my code like this:

this.mBaseOutputStream.write("Лорем ăîîîîîîă".getBytes("cp850"));

But all I get for the non-latin characters are weird symbols. So what am I doing wrong?

Alexandru Pufan
  • 1,842
  • 3
  • 26
  • 44

2 Answers2

5

Not sure this is an answer as such, but hopefully this will get things started. Also need a bit of room to explain...

It looks like code page 850 doesn't have the characters needed. An easy way to check this offline is to convert back to a String. E.g. :

System.out.println(
        new String("Лорем ăîîîîîîă".getBytes("cp850"), "cp850"));
--> ????? ?îîîîîî?

Clearly only the î is available there.

You may need to do some experiments with alternative code pages - what type of printer is this?

A couple of tests here suggest the example string may need more than one code page, but someone else may know better:

System.out.println(
        new String("Лорем ăîîîîîîă".getBytes("cp852"), "cp852"));
--> ????? ăîîîîîîă
System.out.println(
        new String("Лорем ăîîîîîîă".getBytes("cp855"), "cp855"));
--> Лорем ????????
df778899
  • 10,703
  • 1
  • 24
  • 36
0

Send the initialization bytes first(a.k.a write(); flush();) instead of sending all of the data together. Then send your characters.

public void print (String text, String codePage, OutputStream os) 
{
    /*Your codetable initialization here.
     *You can refactor this more efficiently.
     *Hardcoded just so you can understand.
     */
    ByteBuffer init = ByteBuffer.allocate(3);
    init.put((byte) 0x1B);
    init.put((byte) 0x74);
    init.put((byte) 2);
    sendData(init.array(), os);

    ByteBuffer dataToPrint = ByteBuffer.allocate(text.length());
    dataToPrint.put(text.getBytes(codePage));
    sendData(dataToPrint.array(), os);
}

private void sendData(byte[] buffer, OutputStream os) throws IOException
{
    try {
        ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
        os.write(byteBuffer.array());
        os.flush();
        // tell the user data were sent

    } catch (Exception e) {
        e.printStackTrace();
    }
}

You can use it as;

print("Лорем ăîîîîîîă", "cp852", yourOutputStream); // cp852 or any other codePage you desire.

If that doesn't work, try closing the multibyte(single byte fit for europe area) before printing.

ByteBuffer closeMultibyte = ByteBuffer.allocate(2);
closeMultibyte.put((byte) 0x1C);
closeMultibyte.put((byte) 0x2E);
sendData(closeMultibyte.array(), os);
Burak Day
  • 907
  • 14
  • 28