简体   繁体   中英

C# How to marshal string become binary format

I need to print barcode to escpos printer and the command should to follow this rule: $1D $6B m d1...dk $00 .

for example: if i wan to print CODE 39 the command is: write("\x1d\x6b\x04\x43\x4f\x44\x45\x20\x33\x39\x00")

my problem is:

  1. how to convert CODE 39 become \x43\x4f\x44\x45\x20\x33\x39 ?
  2. why this command not work write("\x1d\x6b\x04CODE 39\x00") ?

UPDATE

I use this RawHelperPrinter to print string to my escpos printer. This helper provide two method:

  1. SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  2. SendStringToPrinter(string szPrinterName, string szString)

Question

I can print hard coded barcode ( CODE 39 ) with this command:

SendStringToPrinter("My ESCPOS Printer", "\x1d\x6b\x04\x43\x4f\x44\x45\x20\x33\x39\x00");

but how to print dynamic barcode?

UPDATE 2

as @kunif mentioned, i need to send binary data to create barcode. here is my code to achieve it:

string data = "CODE 39";
byte[] buffer = Encoding.UTF8.GetBytes(data);
IntPtr ptr = Marshal.StringToCoTaskMemAnsi(buffer);
SendStringToPrinter("My ESCPOS Printer", "\x1d\x6b\x04");
SendBytesToPrinter("My ESCPOS Printer", ptr, buffer.Length);
SendStringToPrinter("My ESCPOS Printer", "\x00");

but it's still not working, any ide?

As I wrote in the comment, the escape string \x04CODE is 0x4c, 0x4f, 0x44, 0x45 and as a result \x04C seems to be evaluated as \x4C .
For example, you can check how it is converted by such a console program.

using System;
using System.Linq;
using System.Text;

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            byte[] buffer = Encoding.ASCII.GetBytes("\x1d\x6b\x04CODE 39\x00");
            Console.WriteLine(BitConverter.ToString(buffer));

            buffer = Encoding.ASCII.GetBytes("\x1d\x6b\x04\x43ODE 39\x00");
            Console.WriteLine(BitConverter.ToString(buffer));

            string data = "CODE 39";
            buffer = new byte[] { 0x1d, 0x6b, 0x04 };
            buffer = buffer.Concat(Encoding.ASCII.GetBytes(data)).ToArray();
            buffer = buffer.Concat(Encoding.ASCII.GetBytes("\x00")).ToArray();
            Console.WriteLine(BitConverter.ToString(buffer));

            Console.ReadLine();
        }
    }
}

Also, in the process written in UPDATE 2 , if one ESC/POS print command is requested by dividing it into multiple methods, it seems may not be regarded as one continuous print command.
This is because even if the name is RawPrinterHelper , it is possible to add Prefix/Suffix data that performs preprocessing and cleanup before and after the data.

So you will need to create one ESC/POS print command as one byte array and request printing with one method.
Since it is a byte array print request, you will probably use SendBytesToPrinter() .

By the way, in CODE39 barcodes, it is common to add a start/stop code * before and after the barcode data.
Since there is such an article, please refer to it when dealing with data.
Code 39 barcodes with no start/stop characters

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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