OMGKurtNilsen
OMGKurtNilsen

Reputation: 5098

C# ESC POS Special characters

I'm making a class for ESC POS printing.

It needs to support special norwegian characters: ÆØÅæøå

The problem is that I can't just use them in a string.

"Data to print: ÆØÅæøå" will print as "Data to print: ??????"

According to the documentation these chars prints the special characters i need:

(char)91 prints "Æ"

(char)92 prints "Ø"

(char)93 prints "Å"

(char)123 prints "æ"

(char)124 prints "ø"

(char)125 prints "å"

So my question is: Is there any better way than to do a Replace for each of the characters?

Here is the code that connects to printer and sends data:

        Socket clientSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        clientSock.NoDelay = true;
        IPAddress ip = IPAddress.Parse("192.168.0.11");
        IPEndPoint remoteEP = new IPEndPoint(ip, 9100);
        clientSock.Connect(remoteEP);
        byte[] byData = Encoding.ASCII.GetBytes(buffer);
        clientSock.Send(byData);
        clientSock.Close();

Solved:

Encoding nordic = Encoding.GetEncoding("IBM865");
byte[] byData = nordic.GetBytes(buffer);

Upvotes: 2

Views: 8440

Answers (2)

vgru
vgru

Reputation: 51224

If it is a standard codepage (like code page 865 for Nordic languages), you can use the appropriate encoding:

Encoding nordic = Encoding.GetEncoding("IBM865"); 

Check supported encodings for the Encoding class to see if there is a match. But from the character layout of 865, it looks like you will need to replace characters yourself.

You can create characters mappings using a dictionary, but obviously a large switch/case statement will do just fine for start (you can refactor it if ever get the need one day).

Upvotes: 4

Roy Dictus
Roy Dictus

Reputation: 33139

I don't think ASCII encoding is what you want -- see what happens when you use UTF-8.

Upvotes: 0

Related Questions