Click here to Skip to main content
14,734,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone
I need to convert this List into a byte array to send via serial port. But when I try to use the DataList.SelecMany command I return the error: "Enumerable.SelectMany ..." cannot be inferred based on usage. Try explicitly specifying type arguments".
what i'm forgetting?

What I have tried:

List<string> listaDados = new List<string>();
        //
        int szCmds, wdCnt;
        String stValue;

        MessageBox.Show("Print Cheque ");

        int szCmds, wdCnt;

        String stValue;


        stValue = "748";      //Banco
        listaDados.Add("0x1B");
        listaDados.Add("0xA2");
        listaDados.Add(stValue);

        byte[] data = listaDados.SelectMany(BitConverter.GetBytes).ToArray();

        serialPort.Write(data, 0, data.Length);
Posted
Updated 1-Oct-19 1:26am
v2
Comments
Richard MacCutchan 30-Sep-19 15:40pm
   
Why not use a StringBuilder rather than a List? Also the strings "0x1B" and "0xA2" will not convert to their byte values as you expect, you need to use proper escape sequences "\x1B" and "\xA2".
Gilcecler Carneiro 30-Sep-19 18:45pm
   
Well, the example I took used a list

"https://www.codeproject.com/Questions/1130049/How-to-write-serial-port-using-Csharp"
Maciej Los 1-Oct-19 6:19am
   
Well... Use "Reply" widget (on the right of member name/nick) to be sure that notification system will inform user about your reply.
Richard MacCutchan 1-Oct-19 6:58am
   
That does not mean it is correct.
Gilcecler Carneiro 30-Sep-19 18:50pm
   
about escape sequences my exemplo original was in java.
I will correct
Gilcecler Carneiro 30-Sep-19 19:00pm
   
accepted suggestion ;)
CPallini 1-Oct-19 3:13am
   
Why are you using a list of strings? Such strings are the hexadecimal representation of bytes. Why don't you use directly numbers (possibly hexadecimal ilterals)?
See
https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/integral-numeric-types

1 solution

Assuming that you have a list<string> and you want to convert all string in a list into byte array...

List<string> listaDados = new List<string>();
listaDados.Add("0x1B");
listaDados.Add("0xA2");
listaDados.Add("748");

Encoding  u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();


For further details, please see: Encoding.GetBytes Method (System.Text) | Microsoft Docs[^]

If you would like to convert result into string, use this:
string s = u8.GetString(result);


Note: As Richard MacCutchan mentioned in the comment to the question, you've got wrong escape character for two hex values. I did not correct this, because i wanted to provide example how to convert list of strings into byte array.

Good luck!
   
v3
Comments
Richard MacCutchan 1-Oct-19 6:40am
   
Wrong escape sequences for the two hex values.
Maciej Los 1-Oct-19 6:50am
   
Agree, but i wanted to show how to convert list<string> into byte array.
Richard MacCutchan 1-Oct-19 6:59am
   
Also, when I ran it it adds an arbitrary 0xC2 between the first two values. Any idea why?
Maciej Los 1-Oct-19 7:11am
   
Because of hex string ;)
To convert hex string into byte array it's necessary to add extra conversion: c# - How can I convert a hex string to a byte array? - Stack Overflow[^]
Gilcecler Carneiro 1-Oct-19 14:13pm
   
I corrected, thank you

stValue = "748";
listaDados.Add("\x1B");
listaDados.Add("\xA2");
listaDados.Add(stValue);
Gilcecler Carneiro 1-Oct-19 14:22pm
   
full code.
the idea is to print each block on the serial port
how can i fill in the result variable print, fill and print ... in each block?



instanciaDoForm1 = frm1;

String stValue;
List<string> listaDados = new List<string>();
MessageBox.Show("pay by check");

//Favorecido
stValue = "JHON FAVORECIDO";
listaDados.Add("\x1B");
listaDados.Add("\xA0");
listaDados.Add(stValue);
listaDados.Add("\x0D");
Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();

instanciaDoForm1.Porta.Write(result, 0, result.Length);

//local
stValue = "FRANCISCO BELTRAO";
listaDados.Add("\x1B");
listaDados.Add("\xA1");
listaDados.Add(stValue);
listaDados.Add("\x0D");
Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();
instanciaDoForm1.Porta.Write(result, 0, result.Length);
//Banco
stValue = "748";
listaDados.Add("\x1B");
listaDados.Add("\xA2");
listaDados.Add(stValue);
Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();
instanciaDoForm1.Porta.Write(result, 0, result.Length);
//Valor
stValue = "12350,50";
listaDados.Add("\x1B");
listaDados.Add("\xA3");
listaDados.Add(stValue);
listaDados.Add("\x0D");
Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();
instanciaDoForm1.Porta.Write(result, 0, result.Length);

//Data
stValue = "01/10/2019";
listaDados.Add("\x1B");
listaDados.Add("\xA4");
listaDados.Add(stValue);
listaDados.Add("\x0D");
Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();
instanciaDoForm1.Porta.Write(result, 0, result.Length);

// Inicia a Impressao com dados enviados
listaDados.Add("\x1B");
listaDados.Add("\xB0");
listaDados.Add("\x0D");

Encoding u8 = Encoding.UTF8;
byte[] result = listaDados.SelectMany(x => u8.GetBytes(x)).ToArray();
instanciaDoForm1.Porta.Write(result, 0, result.Length);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900