Click here to Skip to main content
15,847,427 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add two byte array in window application.
C#
byte[] b1;
byte[] b2;

I want to do:-
C#
b2=b1+b2;

How can i do this process with byte array.
I am use this:

for (int i = 0; i <b1.length;>{
b2[posoftotaldata] += b1[i]; // posoftataldata is define globally and static
posoftotaldata++;
}
But in this i don't get exact size of b2 because i also defined b1 and b2 globally how i define dynamically size of b2.
This work properly but confusion in size.
Posted
Updated 19-Dec-11 6:14am
v3

1 solution

A simple way to achieve this is to use a generic list and use the AddRange method to add the byte arrays together - the list allows you to output the data as an array, so you would get a byte array out. Here's an example:
C#
private byte[] GetBytes(byte[] b1, byte[b2])
{
  List<byte> data = new List<byte>();
  data.AddRange(b1);
  data.AddRange(b2);
  return data.ToArray();
}
There are many ways to achieve the same effect, but this is a simple, easy to debug version.
 
Share this answer
 
Comments
jaideepsinh 19-Dec-11 11:08am    
I want to do this process in class file and in class file i don't get List.
Pete O'Hanlon 19-Dec-11 11:13am    
You need to add using System.Collections.Generic to get access to the generic list.
Sergey Alexandrovich Kryukov 19-Dec-11 18:59pm    
What is "in class file"?! What does it mean "I don't get list"? What and how you wanted to "get" it?
--SA
Sergey Alexandrovich Kryukov 19-Dec-11 18:58pm    
Yes, a 5.
--SA
jaideepsinh 20-Dec-11 1:39am    
If i use this method i have same problem for defining size of b2 because i am not remove b2 data every time i want to append data to b2.How i define size of b2.

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