Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / September 2003

Tip: Looking for answers? Try searching our database.

.NET String class and ASCII NUL character question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Brian Quinlan - 25 Sep 2003 20:07 GMT
Hi,

I've encountered a problem when using the String class in C#. Strings seem
to be truncated at the first NUL character. Am I correct?

Cheers,
Brian
David Browne - 25 Sep 2003 20:19 GMT
> Hi,
>
> I've encountered a problem when using the String class in C#. Strings seem
> to be truncated at the first NUL character. Am I correct?

There were some bugs in the 1.0 framework related to strings containing null
characters.

David
Jon Skeet - 25 Sep 2003 23:11 GMT
> > I've encountered a problem when using the String class in C#. Strings seem
> > to be truncated at the first NUL character. Am I correct?
>
> There were some bugs in the 1.0 framework related to strings containing null
> characters.

Only in the debugger, IIRC. I don't remember seeing any posts about
problems in the framework, but there were certainly lots about the
debugger. Of course, I could well have missed some problems.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Mattias Sj?gren - 25 Sep 2003 20:27 GMT
Brian,

>I've encountered a problem when using the String class in C#. Strings seem
>to be truncated at the first NUL character. Am I correct?

No, .NET strings are generally not null terminated. They have a length
prefix and can therefore contain any character. But if you display a
string in a message box or in the debugger, it might be truncated at
the first null character.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Brian Quinlan - 25 Sep 2003 21:25 GMT
> No, .NET strings are generally not null terminated. They have a length
> prefix and can therefore contain any character. But if you display a
> string in a message box or in the debugger, it might be truncated at
> the first null character.

Not only is the display truncated but the length displayed is incorrect:

static void Main(string[] args)
{
 char[] chars = {'\0', 'a', '\0', 'c', '\0'};
 string s = new String(chars);
 System.Console.WriteLine(s.Length);
 byte[] bytes = {0, 97, 0, 99};
 string t = System.Text.Encoding.ASCII.GetString((byte[]) bytes.Clone());
 System.Console.WriteLine(t.Length);
}

If I place a breakpoint on the  byte[] bytes = {0, 97, 0, 99} line, the
debugger will report s as being "" and s.Length as 0. But 5 has been written
to the console! Anybody understand this phenominon?

Cheers,
Brian
David Browne - 25 Sep 2003 21:42 GMT
> > No, .NET strings are generally not null terminated. They have a length
> > prefix and can therefore contain any character. But if you display a
[quoted text clipped - 16 lines]
> debugger will report s as being "" and s.Length as 0. But 5 has been written
> to the console! Anybody understand this phenominon?

Bugs in VS 2002, and/or framework 1.0.

In VS 2003 the debugger shows them as

> ? s.Length
5
> ? s
"\0a\0c\0"
>? t.Length
4
> ? t
"\0a\0c"

David
Jon Skeet - 25 Sep 2003 23:34 GMT
> > No, .NET strings are generally not null terminated. They have a length
> > prefix and can therefore contain any character. But if you display a
[quoted text clipped - 16 lines]
> debugger will report s as being "" and s.Length as 0. But 5 has been written
> to the console! Anybody understand this phenominon?

Yup - the debugger thinks it knows best, so doesn't actually invoke the
real String.Length property.

Are you using VS.NET 2002 or 2003? I thought this had been fixed for
2003...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Brian Quinlan - 27 Sep 2003 00:24 GMT
> Yup - the debugger thinks it knows best, so doesn't actually invoke the
> real String.Length property.

Cute.

> Are you using VS.NET 2002 or 2003? I thought this had been fixed for
> 2003...

I'm still using 2002. I've been eagerly waiting for 2003 to arrive in the
mail for about two months now.

Cheers,
Brian

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.