Thankappan

Hi,
I am receiving a message packet through socket into a byte array. When the message length exceeds a certain limit, the message is coming as two parts at the socket. I want to concatinate the two parts into a single array.
Suppose my array size is 2048 and I am getting 1024 in the first part and another 1024 in the second part at the socket.
1st part - [1024(data) + 1024(zeros)]
2nd part - [1024(data) + 1024(zeros)]
concat - [1024(data) + 1024(zeros) + 1024(data) + 1024(zeros)]
I want two remove the zeros which will come in between the two parts.


Re: Visual C# General Concatinating two byte arrays

Mattias Sjogren

Since arrays have a fixed length, you have to create a new one with the length of the two parts. You can then use for example System.Buffer.BlockCopy to copy the content of the smaller arrays to the larger one.

If this happens repetedly you could also consider using a MemoryStream which will handle much of this for you.






Re: Visual C# General Concatinating two byte arrays

Thankappan

Hi,
Thanks for your suggestion. But i want to know how we can find the length of each part. Because array.Length will give the maximum capacity of the array only. How I can find the length of only the portion which contains data




Re: Visual C# General Concatinating two byte arrays

TilakGopi

Hi, this way u can find the length of only the portion which contains datai

char ch = '\0' ; //whether u mean this

or

char ch = 0; // or this , i'm not sure

int nLen = Encoding.ASCII.GetString(bytearray).TrimEnd(ch).Length;

 

Thanx,

Ch.T.Gopi Kumar.






Re: Visual C# General Concatinating two byte arrays

Mattias Sjogren

No need to convert it to a string. You can do

len = Array.IndexOf(bytearray, (byte)0);






Re: Visual C# General Concatinating two byte arrays

TilakGopi

Today, 12:23 PM UTC

Mattias Sjogren
MVP


Posts 1,235
Re: Concatinating two byte arrays
Was this post helpful

No need to convert it to a string. You can do

len = Array.IndexOf(bytearray, (byte)0);