C# ESC POS 特殊字符
我正在制作 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它是标准代码页(例如北欧语言的代码页 865),您可以使用适当的代码页编码:
检查支持的编码
Encoding
类来查看是否存在匹配。但从 865 的字符布局来看,您似乎需要自己替换字符。您可以使用字典创建字符映射,但显然一个大型 switch/case 语句就可以很好地开始(如果有一天需要的话,您可以重构它)。
If it is a standard codepage (like code page 865 for Nordic languages), you can use the appropriate encoding:
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).
我不认为 ASCII 编码是您想要的——看看使用 UTF-8 时会发生什么。
I don't think ASCII encoding is what you want -- see what happens when you use UTF-8.