Mar 13, 2011 17:39
Things I Did Not Know But Should've Connected The Dots:
.Net strings can contain nulls. So if you have a byte[] like this:
byte[] fooBytes= { 'A', '\0', 'B' };
And use an ASCII encoding to get a system.String like this:
ASCIIEncoding enc = new ASCIIEncoding();
string foo = enc.GetString(fooBytes);
You will end up with a string containing this:
A\0B
when viewed as an intellisense tooltip, and this:
A
when viewed using the 'text visualiser' - the little magnifying glass in VS. Also, foo.Length will return three.
It makes sense when you think about it - null-termination is an implementation detail which should be hidden from the string class - but it results in some really, really nasty potential bugs. I'll be keeping an eye out for this in the future.
c#,
security,
.net