String stops appending after char 00H

I am doing some experiments and as part of the exploratory process I am trying to write a program that would present files in a similar fashion a hex editor would do.

I load a file using FileStream and I write all the bytes to a byte array. I then loop through the array and write the bytes out in a hex format in blocks with a width of my choosing (16 bytes in the case of this example).

Next to each line I want to write the bytes in ASCII format but when I encounter 00H all the text after that is not appended to the string.

The code looping through the bytes is as follows:

while (j < data.Length - 1)
{
    for (int i = 0; i <= blockWidth - 1; i++)
    {
        if (j < data.Length - 1)
        {
            byteBlock[i] = data[j];
            charBlock[i] = Convert.ToChar(data[j]); //ASCII Characters to be appended                       
            j += 1;
        }
    }
    sTemp = new string(charBlock);
    hex += BitConverter.ToString(byteBlock) + "      " + sTemp;//bug-causing line
    hex += Environment.NewLine;
}
textBox1.Text = hex;

The text in textBox1 is only the following line:

D0-CF-11-E0-A1-B1-1A-E1-00-00-00-00-00-00-00-00        ÐÏࡱá

There are 16 bytes of hex data and only 8 ASCII characters (2 of them don't show on this page).

I added some breakpoints and stepped through the code, the loop runs to the end with no problems.

When I comment out the part where I add sTemp to hex like this:

sTemp = new string(charBlock);//Output confirms the bug is not in this line
hex += BitConverter.ToString(byteBlock);// +"      " + sTemp;//bug-causing line
hex += Environment.NewLine;

I get the following output:

D0-CF-11-E0-A1-B1-1A-E1-00-00-00-00-00-00-00-00
00-00-00-00-00-00-00-00-3E-00-03-00-FE-FF-09-00
06-00-00-00-00-00-00-00-00-00-00-00-01-00-00-00
01-00-00-00-00-00-00-00-00-10-00-00-02-00-00-00
03-00-00-00-FE-FF-FF-FF-00-00-00-00-00-00-00-00
FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF-FF...

Could someone please explain to my why this is happening and also what I should do in order to fix it?

Jon Skeet
people
quotationmark

Next to each line I want to write the bytes in ASCII format but when I encounter 00H all the text after that is not appended to the string.

Yes it is - it's just not shown in the text box. (You're also using ISO-8859-1, not ASCII. ASCII doesn't contain any accented characters etc.)

Most Windows Forms controls are very thin wrappers over Win32 controls, and those treat U+0000 as the end of a string.

In your second piece of code, you convert the bytes into text using hex, and then you can see that the data is there.

Basically, you shouldn't try to display non-text as if it's text. If you really, really want to do that, I'd just use

text = text.Replace('\u0000', ' ');

to avoid the Win32 control from "seeing" a Unicode NUL character and stopping.

people

See more on this question at Stackoverflow