代码之家  ›  专栏  ›  技术社区  ›  adopilot

如何像c_中的edit一样对字符串进行编码?

  •  3
  • adopilot  · 技术社区  · 15 年前

    我正在编写C应用程序,需要使用rawprinterhelper将数据打印到pos star打印机。

    我的印刷效果很好,除了我发送像_½___这样的字符。 然后我得到错误的数据打印出来。

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

    如果我在PowerShell中打开良好的旧编辑并在TXT文件中写入我的字符(_½_) 把它送到打印机,我就可以随心所欲地打印出来。

    我不能重复使用记事本

    我如何用C编码我的刺,使其看起来像来自命令提示符(eidtor)的刺。所以 当我将数据发送到打印机时,它会像在Windows环境中那样打印所需的字体。

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

    编辑:


    我做到了,对于其他人来说,他们通常很难用C直接在星型打印机上打印。# 这是示例应用程序的代码 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 回复  |  直到 15 年前
        1
  •  4
  •   Lucero    15 年前

    如果您要使用DOS编辑器,则需要处理旧的编码。

    Have a look at this link: 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