Click here to Skip to main content
15,891,673 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hello CPs,

Today I am doing Network programming where I suddenly hit a rock. I read a group of bytes from a NetworkStream(Server), but the byte array contains data + null characters attached to it.
Suppose:
C#
byte[] buf = byte[100];
string temp;
NetworkStream ns = GetStream(); //doesnt matter

ns.Read(buf, 0, 100);  //say incoming data is hello
temp = Encoding.ASCII.GetString(buf);

now buf[] value is {n,n,n,n,n,'\0','\0','\0'..... 100th element")
temp value is "hello\0\0\0\0\0....100th element"

Now this is messing with my other calculations.
Is there a method to remove the null characters?
I tried using Trim but it didn't work.

Thanks in Advance if u solve this :)
Posted
Updated 3-Feb-14 7:45am
v2

Nulls in a byte array are just 0's. I'm suspect of what you're calling "calculations" and how zero values would screw those up.

What nobody has mentioned yet is that NetworkStream.Read() returns an integer, which you're ignoring. That integer is the number of characters read by the Read method and stored in the buffer.

Depeding on what your "calculations" are, you may be better off using that value instead of wasting time replacing a bunch of 0 characters with String.Empty.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 3-Feb-14 16:58pm    
Exactly, a 5. Zero bytes should never be removed from data to be parsed into a Unicode strings, as the characters are not 1-bytes ones. The results of that could be unpredictable.
That could happen with all other "answers". OP should think at how the byte[] buff appeared in first place...
—SA
Matt's solution is correct for removing null characters from a string (+5 to Matt)

There is something else you may consider here, I see that you are using ASCII enoding which is limited to 128 characters. You may want to check that you are using the correct encoding for your data stream. I couldn't tell the amount of hair pulling and calling upon deities that I have seen from people when attempting to debug their application only to find they were using the wrong encoding scheme.

This remark is from MS:
"The ASCIIEncoding object that is returned by this property might not have the appropriate behavior for your application"
Source: Encoding.ASCII Property [^]

Good luck.
 
Share this answer
 
C#
temp = temp.Replace("\0", string.Empty);

will remove the null characters.
 
Share this answer
 
Quote:
You can use Linq in case you are using .NET 3.5 or later:

temp = temp.Where(x => !string.IsNullOrEmpty(x)).ToArray();

If you can't use Linq then you can do it like

var tempvar = new List<string>();
foreach (var s in test)
{
if (!string.IsNullOrEmpty(s))
temp.Add(s);
}
temp = tempvar.ToArray();
 
Share this answer
 
Thanks you all for your kind reply and time you took to answer my question.
 
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