|
-
Aug 19th, 2004, 10:27 AM
#1
Thread Starter
Addicted Member
Binary File Data / Textbox
I'm new to C#, and the first program that I'm doing is a clone of Notepad. This basically consists of a TextBox control docked on a form.
All was going well until I tried to open something other than a text file (namely a bitmap). The problem is that certain characters ('\0' for example) won't display, and worse still won't allow any other characters to appear after them.
I think this is a limitation of the TextBox control, but I'm not sure. This is how I'm reading the file data:
Code:
BinaryReader file = new BinaryReader (File.Open (dlgOpenFile.FileName, FileMode.Open, FileAccess.Read));
byte [] buffer = new byte [1024];
while (file.BaseStream.Position != file.BaseStream.Length)
{
int bytesRead = file.Read (buffer, 0, 1024);
string text = System.Text.Encoding.ASCII.GetString (buffer, 0, bytesRead);
txtText.Text += text;
}
file.Close ();
This seems to read the file data properly, but when it is added to the TextBox (txtText) object, it all seems to go wrong.
So, am I reading the file properly? Is the textbox control capable of displaying these characters? Does anyone know where I'm going wrong?
Thanks.
Using Visual Studio .NET 2005
-
Aug 30th, 2004, 03:35 AM
#2
yay gay
Open it with File.OpenRead() instead
\m/  \m/
-
Aug 30th, 2004, 02:10 PM
#3
No, the textbox can't display a null character -- think about how strings of text are handled in memory: a character array, followed by a null character to indicate the end of the string. When the textbox encouters a null character, it interprets that as the end of the displayable text. If you want to display binary data, you should display it in hexidecimal format or similar way.
Good luck!
Last edited by sunburnt; Aug 30th, 2004 at 02:32 PM.
Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|