Click here to Skip to main content
15,907,326 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I merge bytes format

string s = textBox1.Text;
            byte[] bytes = Encoding.ASCII.GetBytes(s);            

            //byte[] bytesToSend = new byte[16] { 0x01, 0x46, 0x42, 0x44, 0x2d, 0x2d, 0x2d, 0x53, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x17};
            byte[] bytesToSend = new byte[7] { 0x01, 0x46, 0x4d, 0x42, 0x2d, 0x2d, 0x2d };
            byte b3 = (byte)(bytesToSend & bytes);

            clientSocket.Send(b3);


thank you

What I have tried:

I tryid operators & and + but still error CS0019 Operator '&' cannot be applied to operands of type 'byte[]' and 'byte[]'
Posted
Updated 7-Feb-19 22:10pm

You can't AND whole arrays: AND is an operator that works on individual elements of the array, as it "+". If that you want to do is combine the two arrays so your get one bigger array with all the elements of one followed by all the elements of the other, then do this:
byte[] a1 = new byte[16] { 0x01, 0x46, 0x42, 0x44, 0x2d, 0x2d, 0x2d, 0x53, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x17};
byte[] a2 = new byte[7] { 0x01, 0x46, 0x4d, 0x42, 0x2d, 0x2d, 0x2d };
byte[] combined = a1.Concat(a2).ToArray();
 
Share this answer
 
Comments
Member 13711215 8-Feb-19 2:20am    
Sorry, I try to merge byte[] bytes = Encoding.ASCII.GetBytes(s);

and

byte[] bytesToSend = new byte[7] { 0x01, 0x46, 0x4d, 0x42, 0x2d, 0x2d, 0x2d };
OriginalGriff 8-Feb-19 2:40am    
Then you need to specify *exactly* what you expect to get as a result.
If you have two arrays
byte[] a1 = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
byte[] a2 = new byte[] { 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70};
Then exactly what do you expect the "merger" to produce and why? There are a huge number of possibilities!
Member 13711215 8-Feb-19 2:54am    
In textbox1 is text =00990202. I want merge byte row "bytesToSend" with textbox1 text to one row and send this row via clientsocket. Bytetosend + textbox1 and send this full row.
OriginalGriff 8-Feb-19 3:17am    
That tells me nothing.
I have no idea where the string "00990202" is supposed to come from or how you need to "merge" the bytes with it.

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. So stop trying to type as little as possible, or we will be here all day! :laugh:
My solution

string s = textBox1.Text;
            var bytes = Encoding.ASCII.GetBytes(s);            

            //načtení souboru
            byte[] bytesToSend = new byte[8] { 0x01, 0x46, 0x4d, 0x42, 0x2d, 0x2d, 0x2d, 0x52 };
            byte[] bytesToSend2 = new byte[1] {0x17};           

            var load = bytesToSend.Concat(bytes).ToArray();
            var load2 = load.Concat(bytesToSend2).ToArray();
            clientSocket.Send(load2);        

            //print souboru
            System.Threading.Thread.Sleep(1000);
            byte[] print = new byte[16] { 0x01, 0x46, 0x42, 0x44, 0x2d, 0x2d, 0x2d, 0x53, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x17 };
            clientSocket.Send(print);

            clientSocket.Close();
 
Share this answer
 

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