2

I have big problem... My piece of code:

string doc = System.Text.Encoding.ASCII.GetString(stream);

Variable doc is ending at first null (/0) character (a lot of data is missing at this point). I want to get whole string. What's more, when I copied this piece of code and run in immediate window in Visual Studio - everything is fine...

What I'm doing wrong?

5
  • What is stream exactly? Commented Mar 31, 2015 at 7:04
  • stream is just byte array
    – GrzesiekO
    Commented Mar 31, 2015 at 7:06
  • 2
    is the length of byte array as you expected?
    – Aik
    Commented Mar 31, 2015 at 7:08
  • Doesn't this overload exist: System.Text.Encoding.ASCII.GetString(byte[] bytes, int byteIndex, int byteCount) where bytes is your byteArray to read from, byteIndex is the Index of the byte you want to start reading from and byteCount is the amount of bytes you want to read in total ? Use this with byteIndex = 0 and byteCount = stream.length. Commented Mar 31, 2015 at 7:17
  • I've used this overload too. Result is the same as previous.
    – GrzesiekO
    Commented Mar 31, 2015 at 7:29

1 Answer 1

11

No, it doesn't:

string doc = System.Text.Encoding.ASCII.GetString(new byte[] { 65, 0, 65 }); // A\0A
int len = doc.Length; //3

But Winforms (and Windows API) truncate (when showing) at first \0.

Example: https://dotnetfiddle.net/yjwO4Y

I'll add that (in Visual Studio 2013), the \0 is correctly showed BUT in a single place: if you activate the Text Visualizer (the magnifying glass), that doesn't support the \0 and truncates at it.

Why this happens? because historically there were two "models" for string, C-strings that are NUL (\0) terminated (and so can't use \0 as a character) and Pascal strings that have the length prepended, and so can have the \0 as a character. From the wiki

Null-terminated strings were produced by the .ASCIZ directive of the PDP-11 assembly languages and the ASCIZ directive of the MACRO-10 macro assembly language for the PDP-10. These predate the development of the C programming language, but other forms of strings were often used.

Now, Windows is written in C, and uses null terminated strings (but then Microsoft changed idea, and COM strings are more similar to Pascal strings and can contain the NUL character). So Windows API can't use the \0 character (unless they are COM based, and probably quite often the COM based could be buggy, because they aren't fully tested for the \0). For .NET Microsoft decided to use something similar to Pascal strings and COM strings, so .NET strings can use the \0.

Winforms is built directly on top of Windows API, so it can't show the \0. WPF is instead built "from the ground up" in .NET, so in general it can show the \0 character.

6
  • It is .net framework specific function not WPF. It always behaves as you described.
    – Aik
    Commented Mar 31, 2015 at 7:08
  • WPF actually does show the null character when its data-bound to a control.
    – poke
    Commented Mar 31, 2015 at 7:09
  • 1
    Also, in your dotnetfiddle example, there is nothing truncated either. The null character is just skipped in the output. But it’s still showing AA (maybe “truncated” is the wrong word?).
    – poke
    Commented Mar 31, 2015 at 7:14
  • @poke Yep... And tested the WPF... With Label, TextBox and Button the \0 is showed as a "missing character" square. Clearly you can't use it in the title of the window, because the title is drawed by Windows
    – xanatos
    Commented Mar 31, 2015 at 7:19
  • 2
    @Aik Actually, it's the other way round. .NET framework does handle \0 just fine, it's the interop code that doesn't. This is easily evidenced by the fact that all the string methods work correctly - you can IndexOf('\0'), Substring, Split('\0')... The trouble comes when you have to convert the string to a null-terminated char array - obviously, everything after the first '\0' is left out. It's a limitation of null-terminated strings, not .NET - .NET's strings aren't null-terminated.
    – Luaan
    Commented Mar 31, 2015 at 7:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.