2

For some reason, the byte array output of BeginReceive fills up with nulls and then the data.

BeginReceive:

AsyncResult = connectSocket.BeginReceive(RecvBuffer2, 0, RecvBuffer2.Length, SocketFlags.None, OnDataRecvCallback, null); 

Byte Array declaration:

public static byte[] RecvBuffer2 = new byte[9999]; 

How to remove the nulls and keep the rest of the data?

3
  • Could the 'real' data start with a 0x00? What (sort of) data do you expect? Commented Aug 12, 2010 at 10:12
  • No it can't. It is plain text (ascii) data. Commented Aug 12, 2010 at 10:56
  • 1
    OK, still be careful about byte-order marks. Commented Aug 12, 2010 at 12:00

3 Answers 3

3

Should be doable with LINQ. Untested, since I don't have Visual Studio available right now, but it should be something like this:

var usefulBuffer = RecvBuffer2.SkipWhile(x => x == 0).ToArray() 
Sign up to request clarification or add additional context in comments.

3 Comments

This answer worked but I had to Reverse (before and after SkipWhile) the array to make it work. Thank You. Answer accepted!
dim newBuffer1 = newBuffer.SkipWhile(x => x == 0).ToArray() above one is showing error near == on hover on error showing Expression expected.
@picnic4u: The VB syntax would be SkipWhile(Function(x) x = 0).
1

We can modify Heinzi's Answer to

 RecvBuffer2= RecvBuffer2.Where(x => x != 0).ToArray(); 

Now there is no need to reverse the array.

The key to the solution is c# predicate.

1 Comment

This worked for me "Encoding.ASCII.GetString(model.Timestamp.Where(x => x != 0).ToArray()).TrimEnd();"
0

This modification of Heinzi's answer also works:

var usefulBuffer = RecvBuffer2.TakeWhile(x => x != 0).ToArray(); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.