Click here to Skip to main content
15,896,154 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 0: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

1 solution

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

C#
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:
C#
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!
 
Share this answer
 
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);

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