Search code examples
javaeclipseencodingcyrillicfiscal

Eclipse printing wrong cyrillic character


I am making an app that uses fiscal printer, and the document that i send to the printer must be in Cyrillic. The problem is that one specific character (one for the tariff group because i am trying to print out a receipt) should be (char)192, but somehow in the process gets changed to some random character. I've tried changing the encoding of the project to UTF-8 under Project>Properties>Resource>Text file encoding and nothing changed. I've also tried changing the encoding in Window>Preferences>General>Workspace>Text file encoding to UTF-8 and still nothing.

Note: when I type the document manually on other machines and then send it to the fiscal printer it works fine (but not on my pc though). I use notepad to edit the file the output file type is .in.

Here is the code

if(result==JOptionPane.YES_OPTION){
    try {
        PrintWriter writer;
        writer = new PrintWriter("PF500.in");

        String line1 = " 01,0000,1";
        writer.println(line1);
        String etq = "#1";
        String line2 = null;
        String tarifa = null;
        for(Artikli art : list){
            switch(etq){
                case "#1": etq = "$1";
                break;
                default: etq = "#1";
            }
            switch(art.tarifa){
                case "0801": tarifa = Character.toString((char)192);
                break;
                case "0701": tarifa = Character.toString((char)193);
                break;
                case "0601": tarifa = Character.toString((char)194);
                break;
            }
            line2 = etq + art.name.trim() + Character.toString((char)9) + tarifa + art.cena + "*" + art.kolicina;
            writer.println(line2);
        }
        writer.println("%5" + Character.toString((char)9) + "P" + String.valueOf(total));
        writer.println("#" + Character.toString((char)56));

        writer.close();
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
    }               

    con.clearSmetka(id);
    con.insertIzvestaj(list, den.date, id, user.name, time, popust);
    dtm1.setRowCount(0);
    smetkaTable.setModel(dtm1);
    btnCloseSmetka.setEnabled(false);   
    btn.setBackground(new Color(0, 128, 128));
    btn.setText("Маса " + String.valueOf(id));
    smetkaTxt.setText("0,00");
    workFrame.dispose();
}

Everything works fine except the character for the tariff group (which is (char)192)


Solution

  • Problem:

    In

    writer = new PrintWriter("PF500.in");
    

    a PrintWriter is created

    which will encode characters using the default charset for this instance of the Java virtual machine.

    This means that in

    writer.println(line2);
    

    the string line2 that contains the substring "À" (which is the same as "\u00c0" or Character.toString((char)192)) is converted to bytes based on the default charset/encoding (which itself depends on the language setting of the operating system).

    Solutions:

    • To directly write the byte 192 without converting it you can use a FileOutputStream instead of the PrintWriter and the method write(int b):
    FileOutputStream writer = new FileOutputStream("PF500.in");
    ...
    writer.write('\u00c0');  
    
    • If you want to continue using the PriterWriter instead, you must specify the encoding/charset that is used by the printer: PrintWriter(File file, String csn).

    • Specify the charset/encoding of the printer when running Java via -Dfile.encoding=... (which could lead to encoding problems elsewhere).

    • As a fourth option, which is not recommended and may work or may not work, you can do an inverse conversion first:

    case "0801": tarifa = new String(new byte[]{(byte)192});