如何在C#中对编辑等字符串进行编码?

时间:2010-06-24 08:14:45

标签: c# encoding

我正在编写需要使用RawPrinterHelper将数据打印到POS STAR打印机的C#应用​​程序。

我的打印工作正常,除非我发送像ŽĆČĐŠ这样的字符。 然后我打印出错误的数据。

到目前为止,我的研究给了我以下结果。

如果我在PowerShell中打开旧编辑并在txt文件中写下我的字符(ŽĆČĐŠ) 并将其发送到打印机我按照自己的意愿打印出来

我不能重复使用记事本

我如何在C#中将我的sting编码为从命令提示符(EIDTor)看起来像那个。所以 当我向打印机发送数据时,它会在Windows环境中打印出Desire字体。

我也尝试使用Star驱动程序和他们的C#样本进行打印,直接将数据发送到打印机但没有成功。

编辑:


我做到了,对于其他人一般在使用C#直接在Star打印机上打印时遇到麻烦 这是来自Star IO Programming Tool的示例应用程序的代码,用于使用其驱动程序。

using System;
using System.Text;
using StarMicronics.StarIO; // added as a reference from the "Dependencies" directory
                            // requires StarIOPort.dll, which is copied to the output directory by the Post-Build event

namespace TestEnkodera
{
    class Program
    {
        static void Main(string[] args)
        {
            string portName = "LPT1";
            string portSettings = string.Empty;
            string print = string.Empty;

            //Select code page  
            //Decimal  27  29  116  n
            print += string.Format("{0}{1}{2}{3}{4}", (char)27, (char)29, (char)116, (char)5, Environment.NewLine);

            print += "Đ Š Ž Ć Č ž ć č ć \n";            

            IPort port = null;
            port = StarMicronics.StarIO.Factory.I.GetPort(portName, portSettings, 10 * 1000);
            //byte[] command = ASCIIEncoding.ASCII.GetBytes(print); //This was orginal code provided by STAR

            Encoding ec = Encoding.GetEncoding(852); //Here is way to set CODEPAGE to match with printer CODE PAGE
            byte[] command = ec.GetBytes(print);
            uint totalSizeCommunicated = WritePortHelper(port, command);
            StarMicronics.StarIO.Factory.I.ReleasePort(port);
            Console.ReadKey();
        }
        private static uint WritePortHelper(IPort port, byte[] writeBuffer)
        {
            uint zeroProgressOccurances = 0;
            uint totalSizeCommunicated = 0;
            while ((totalSizeCommunicated < writeBuffer.Length) && (zeroProgressOccurances < 2)) // adjust zeroProgressOccurances as needed
            {
                uint sizeCommunicated = port.WritePort(writeBuffer, totalSizeCommunicated, (uint)writeBuffer.Length - totalSizeCommunicated);
                if (sizeCommunicated == 0)
                {
                    zeroProgressOccurances++;
                }
                else
                {
                    totalSizeCommunicated += sizeCommunicated;
                    zeroProgressOccurances = 0;
                }
            }
            return totalSizeCommunicated;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

如果您正在使用DOS编辑器,那么您正在处理遗留编码。

请看一下这个链接:http://msdn.microsoft.com/en-us/library/cc488003.aspx

有时您将字符转换为字节以将其发送到打印机。您可能使用StreamWriter左右这样做。现在你必须为这个“转换器”提供正确的编码,它最终会将字符转换为打印机的正确字节表示,以便扩展字符匹配。

编码列表(对于DOS代码页)可以在这里找到:http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx - 常见的是ibm850(西欧)。

编辑:我在网上找到了以下信息,也许有帮助(我实际上认为代码页0“正常”是#850 Latin-1):

Star Micronics Character Code Tables Reference

Code Page | Description 
0         | Normal
1         | #437 USA, Std Europe
2         | Katakana
3         | #437 USA, Std Europe
4         | #858 Multilingual
5         | #852 Latin-2
6         | #860 Portuguese
7         | #861 Icelandic
8         | #863 Canadian French
9         | #865 Nordic
10        | #866 Cyrillic Russian
11        | #855 Cyrillic Bulgarian
12        | #857 Turkey
13        | #852 Israel
14        | #864 Arabic
15        | #737 Greek
16        | #851 Greek
17        | #869 Greek
18        | #929 Greek
19        | #772 Lithuanian
20        | #774 Lithuanian
21        | #874 Thai