How to convert and send joined or hexdecimal commands in C #

1

I have a dot matrix printer to print and I need to send a cut command to the printer from a form (Windows Forms):

  • Through an interface I select the name of the printer.
  • I have a textbox which I input command in decimal to cut paper. For example: 27,109.0
  • The problem is that said command (27,109,0) and it was converted into hexadecimal like this: \ x1b \ x6D \ x0 through a function, but the problem is that it only works from code, I explain:

    Function Functional Print:

        public void Imprimir()
        {
            string texto = "Prueba de impresion";
            texto += "\x1b\x6D\x0"; // funciona si solo excribo asi en una variable
            Imprimir(nombre_impresora, texto);
        }
    

    and when I try to call the conversion function the converted value comes out like this: \\ x1b \\ x6D \\ x0 with double bar.

    public void Imprimir()
        {
            string texto = "Prueba de impresion";
            texto += ValorHexadecimal("27,191,0"); // retorna \x1b\x6D\x0
            Imprimir(nombre_impresora, texto);
        }
    

    does not work since it has a double bar

    conversion to hexadecimal function:

    public static string ValorHexadecimal(string valor)
        {
            string hex = "";
            string[] valores = valor.Split(new string[] { "," }, System.StringSplitOptions.RemoveEmptyEntries);
            foreach (string c in valores)
            {
                 System.Convert.ToString(int.Parse(c), 16).ToUpper();
                 hex += @"\x"+ System.Convert.ToString(int.Parse(c), 16).ToUpper();;
            }
    
            return hex;
        }
    

    Now how to send decimal values read from a text to a port in hex or unicode:

    P.D. 1: Equivalences of VALUES:

    Decimal | Unicode | Hexadecimal

    27,109.0 | \ u001bm \ 0 | \ x1B \ x6D \ x00

    P.D. 2: The Print function is of the RawPrinterHelper class and works

        
    asked by Raúl 19.10.2018 в 22:37
    source

    1 answer

    0

    I'm going to split the answer into parts.

  • The function you have makes a textual representation of the hexadecimal value, but it does not return a hexadecimal as such. (if that existed).

  • What you need is a string that contains the characters represented by those values.

    For the latter, you can form an array of bytes and then code them in a character string, using ASCII or another encoder. In the case of ASCII, let's assume that you already have an array of bytes with the values {27,109,0} , the encoding It would be something like:

    Byte[] buf = new byte[] {27, 109, 0};
    string comando = System.Text.Encoding.ASCII.GetString(buf);
    Imprimir(nombre_impresora, comando);
    

    The string comando has the characters represented by the decimal values 27, 109 and 0, which is what is sent to print.

  • What you would have left is to make a small routine that returns an array of bytes based on the text with decimals entered by the user.

        
    answered by 19.10.2018 в 23:00