2

My program reads a DDS image file and stores it as a byte array. I want to be able to show the users the raw data in a TextBox form so at first I convert the byte array to a string using the following code:

string data = System.Text.Encoding.ASCII.GetString(bytes);

I then set the TextBox text:

textBox.Text = data;

The problem I am having is the text box is not showing all the data. Here is a screenshot of how it looks:

TextBox view

As you can see only the first few characters are displayed. I am assuming this is because the string contains a null terminator which the TextBox interprets as the end of the string. Here is a copy paste of the first 50 or so characters in the string which I copied directly from the debugger watch window:

DDS |\0\0\0\a\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\

As you can see the first null character comes right after "DDS |" which explains why that's all that shows up in the TextBox.

What I want to be displayed is similar to what you see if you edit the raw DDS file with a text editor such as Notepadd++.

Opening the DDS file in Notepad++ produces the following:

Notepad++ view

My question is, how do I get my TextBox (or RichTextBox) to show the data in the same way that Notepad++ shows it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Jan Tacci
  • 3,131
  • 16
  • 63
  • 83
  • Have you tried using the String.Replace function to swap out the null characters for something else? – called2voyage Jun 27 '13 at 20:29
  • No I have not but that's a good idea. I assume Notepad++ is doing something similar. Or even fancier, drawing special symbols for things that cannot be displayed (the symbol for null, for example). – Jan Tacci Jun 27 '13 at 20:31
  • According to the accepted answer [here](http://forums.asp.net/t/1641116.aspx/1) you should be able to do something like this: `Replace("\0", @"\0")`. – called2voyage Jun 27 '13 at 20:32
  • I have never seen the @ symbol used like that before. What does it mean to put @ in front of a string such as you have shown? – Jan Tacci Jun 27 '13 at 20:34
  • Using the @ in front of the string causes the escape sequence to be ignored. It will display as is. – called2voyage Jun 27 '13 at 20:36

1 Answers1

2

The simplest solution is to use this:

textbox.Text = data.Replace("\0", @"\0");

This will force the textbox to actually show a backslash followed by a zero where the nulls would be. Alternatively, you could replace the nulls with some other character or string.

called2voyage
  • 252
  • 9
  • 28