简体   繁体   English

如何将转义字符从XML的字符串转换为ANSI

[英]How to convert escape characters to ANSI from string in XML

When i use a Windows service written in C# to send a raw string to a receipt printer that uses escape sequences to initiate features within the printer. 当我使用用C#编写的Windows服务将原始字符串发送到使用转义序列在打印机中启动功能的收据打印机时。 Such as: 如:

\\x1b\\x64\\x02

to operate the paper cutter. 操作切纸器。

I'm using a receipt template file on another server and placing the text, base 64 encoded, into XML to give it to the Windows service. 我在另一台服务器上使用收据模板文件,并将以64为基数编码的文本放入XML中,以将其提供给Windows服务。 The problem is that the text is literally printed on the receipt when extracting it from the XML and it's something to do with the escape characters not being translated. 问题在于,从XML中提取文本时,文本实际上是打印在收据上的,这与转义字符未翻译有关。

I'm guessing that when i type those escape sequences into Visual Studio, it represents the ANSI character right there in the string (because it works when i do it that way and then the paper cutter triggers). 我猜想当我在Visual Studio中键入这些转义序列时,它代表字符串中的ANSI字符(因为当我以这种方式执行操作然后切纸器触发时,它可以工作)。

How would i translate the string containing escape sequences into the proper format for use within the C# Windows service? 我如何将包含转义序列的字符串转换为在C#Windows服务中使用的正确格式? The goal is to type these simple escape sequences into a template file and then convert them properly on the other end. 目标是将这些简单的转义序列键入模板文件,然后在另一端正确转换它们。

C# code i have at the moment that is trying to decode base 64 and then convert to ANSI encoding: 我目前正在尝试解码base 64,然后转换为ANSI编码的C#代码:

byte[] data = Convert.FromBase64String(content);
content = Encoding.Default.GetString(data);

byte[] utf8Bytes = Encoding.UTF8.GetBytes(content);
content = Encoding.GetEncoding("Windows-1252").GetString(utf8Bytes);

return RawPrinterHelper.SendStringToPrinter(getConfig().receiptPrinter, content);

Here is the content of the test receipt that is received by the service: 这是服务接收的测试收据的内容:

XHgxYlx4MWRceDYxXHgxIE1OSG9tZU91dGxldC5jb20yMzAwIFdlc3QgSGlnaHdheSAxMw0KQnVybnN2aWxsZSwgTU4gNTUzMzcNCjk1Mi0yNzktMTU4Nw0KDQpceDFiXHgxZFx4NjFceDBceDFiXHg0NFx4Mlx4MTBceDIyXHgwIERhdGU6IDIwMTctMDYtMTVceDkgVGltZTogMzo0NmFtLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tIA0KXHgxYlx4NDUgU0FMRSANClx4MWJceDQ2

When i base 64 encode \\x7 for example, 例如,当我使用base 64编码\\x7

  • From within the service: Bw== (as ASCII) 从服务内部: Bw== (作为ASCII)
  • From within the service: BwA= (as Unicode) 从服务内部: BwA= (作为Unicode)
  • From a string in php: XHg3 从php中的字符串: XHg3

So i've found a solution that requires a small amount of change to the template: 因此,我找到了一个需要对模板进行少量更改的解决方案:

On the template side, using PHP, I convert the \\x7 style unicode reference to  在模板方面,使用PHP将\\x7样式的unicode引用转换为 and then before i base 64 encode it: 然后在我基于64位编码之前:

$receipt_str = mb_convert_encoding($receipt_str, 'UTF-8', 'HTML-ENTITIES');

This is working so far. 到目前为止,这是可行的。 I'm just not sure if there is a better way or if there are limits to this. 我只是不确定是否有更好的方法或对此是否有限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM