'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 ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source