Results 1 to 3 of 3

Thread: Binary File Data / Textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2002
    Posts
    195

    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

  2. #2
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    Open it with File.OpenRead() instead
    \m/\m/

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    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
  •  



Click Here to Expand Forum to Full Width