C# ESC POS 特殊字符

发布于 2024-11-04 02:18:26 字数 965 浏览 0 评论 0原文

我正在制作 ESC POS 打印课程。

它需要支持特殊的挪威语字符:ÆØÅæøå

问题是我不能只在字符串中使用它们。

“要打印的数据:ÆØÅæøå”将打印为“要打印的数据:??????”

根据文档,这些字符打印我需要的特殊字符:

(char)91 打印“Æ”

(char)92 打印“Ø”

(char)93 打印“Å”

(char)123 打印“æ”

(char)124 打印"ø"

(char)125 打印 "å"

所以我的问题是: 有没有比对每个字符进行替换更好的方法?

这是连接到打印机并发送数据的代码:

        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();

已解决:

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

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北方。的韩爷 2024-11-11 02:18:26

如果它是标准代码页(例如北欧语言的代码页 865),您可以使用适当的代码页编码:

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

检查支持的编码 Encoding 类来查看是否存在匹配。但从 865 的字符布局来看,您似乎需要自己替换字符。

您可以使用字典创建字符映射,但显然一个大型 switch/case 语句就可以很好地开始(如果有一天需要的话,您可以重构它)。

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).

眼角的笑意。 2024-11-11 02:18:26

我不认为 ASCII 编码是您想要的——看看使用 UTF-8 时会发生什么。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文