[Mono-list] Socket code.

George Farris george@gmsys.com
Sun, 22 Feb 2004 11:10:38 -0800


Right, I did go and read up on GetString and you are of course correct.

On Sun, 2004-02-22 at 10:51, Jonathan Pryor wrote:
> Below...
> 
> On Sun, 2004-02-22 at 13:06, George Farris wrote:
> <snip/>
> > > Shouldn't it be:
> > > 
> > >  	s = Encoding.ASCII.GetString(bytes, 0, len);
> > >  	buf.Append(s);
> > > 
> > 
> > Well the docs on GetString say it can be just a byte[].  
> 
> Yes, GetString(byte[]) exists, but it's equivalent to this:
> 
> 	string GetString (byte[] bytes)
> 	{
> 		return GetString (bytes, 0, bytes.Length);
> 	}
> 
> So this is only valid if `bytes' is full.  Otherwise (when you've read
> fewer than `bytes.Length' bytes) the end of your string will contain
> garbage data.  The `GetString (bytes, 0, len)' fixes this, so GetString
> will only convert valid data.
> 
>  - Jon
-- 
George Farris <george@gmsys.com>