Page 1 of 1

BinaryReader in C# acting oddly

Posted: Sun Mar 15, 2009 6:41 pm
by HaLo2FrEeEk
Ok, I took some people's advice and I'm using C# now. I'm trying to open a CON file and read it using a BinaryReader. I set the starting position to 0x0000E000 and I told it to read ahead 800 bytes as a test:

Code: Select all

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //setup reader
                FileStream fs = new FileStream(ofd.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                long length = fs.Length;
                //example read
                br.BaseStream.Position = 0x0000E000;
                byte[] example = br.ReadBytes(800);
                TextBox1.Text = BitConverter.ToString(example);
            }
        }
    }
}
If I print it to the textbox like this I get the array in hex format with dashes (-) between the characters. If I use this though:

TextBox1.Text = Encoding.Default.GetString(example);

It prints out the first few characters of the JPEG header...but only up to the first 00 byte, then it won't print anything out anymore. So here's what I'd get for the first method:

Code: Select all

FF-D8-FF-E0-00-10-4A-46-49-46-00-01-01-00-00-01-00-01-00-00-FF-DB-00-43-00-08-06-06-07-06-05-08-07-07-07-09-09-08-0A-0C-14-0D-0C-0B-0B-0C-19-12-13-0F-14-1D-1A-1F-1E-1D-1A-1C-1C-20-24-2E-27-20-22-2C-23-1C-1C-28-37-29-2C-30-31-34-34-34-1F-27-39-3D-38-32-3C-2E-33-34-32-FF-DB-00-43-01-09-09-09-0C-0B-0C-18-0D-0D-18-32-21-1C-21-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-32-FF-C0-00-11-08-02-D0-05-00-03-01-22-00-02-11-01-03-11-01-FF-C4-00-1F-00-00-01-05-01-01-01-01-01-01-00-00-00-00-00-00-00-00-01-02-03-04-05-06-07-08-09-0A-0B-FF-C4-00-B5-10-00-02-01-03-03-02-04-03-05-05-04-04-00-00-01-7D-01-02-03-00-04-11-05-12-21-31-41-06-13-51-61-07-22-71-14-32-81-91-A1-08-23-42-B1-C1-15-52-D1-F0-24-33-62-72-82-09-0A-16-17-18-19-1A-25-26-27-28-29-2A-34-35-36-37-38-39-3A-43-44-45-46-47-48-49-4A-53-54-55-56-57-58-59-5A-63-64-65-66-67-68-69-6A-73-74-75-76-77-78-79-7A-83-84-85-86-87-88-89-8A-92-93-94-95-96-97-98-99-9A-A2-A3-A4-A5-A6-A7-A8-A9-AA-B2-B3-B4-B5-B6-B7-B8-B9-BA-C2-C3-C4-C5-C6-C7-C8-C9-CA-D2-D3-D4-D5-D6-D7-D8-D9-DA-E1-E2-E3-E4-E5-E6-E7-E8-E9-EA-F1-F2-F3-F4-F5-F6-F7-F8-F9-FA-FF-C4-00-1F-01-00-03-01-01-01-01-01-01-01-01-01-00-00-00-00-00-00-01-02-03-04-05-06-07-08-09-0A-0B-FF-C4-00-B5-11-00-02-01-02-04-04-03-04-07-05-04-04-00-01-02-77-00-01-02-03-11-04-05-21-31-06-12-41-51-07-61-71-13-22-32-81-08-14-42-91-A1-B1-C1-09-23-33-52-F0-15-62-72-D1-0A-16-24-34-E1-25-F1-17-18-19-1A-26-27-28-29-2A-35-36-37-38-39-3A-43-44-45-46-47-48-49-4A-53-54-55-56-57-58-59-5A-63-64-65-66-67-68-69-6A-73-74-75-76-77-78-79-7A-82-83-84-85-86-87-88-89-8A-92-93-94-95-96-97-98-99-9A-A2-A3-A4-A5-A6-A7-A8-A9-AA-B2-B3-B4-B5-B6-B7-B8-B9-BA-C2-C3-C4-C5-C6-C7-C8-C9-CA-D2-D3-D4-D5-D6-D7-D8-D9-DA-E2-E3-E4-E5-E6-E7-E8-E9-EA-F2-F3-F4-F5-F6-F7-F8-F9-FA-FF-DA-00-0C-03-01-00-02-11-03-11-00-3F-00-F2-3F-EC-E6-F9-17-CC-24-7A-EC-E9-47-D8-A4-55-03-71-5C-77-C7-F8-D6-E6-51-7E-EB-A2-8F-AD-27-98-07-FC-BD-1F-C0-8F-F0-AC-AE-69-62-95-9D-BC-B0-CC-24-C1-DB-82-0E-7E-94-4E-36-A4-98-60-BC-AE-06-7A-F3-FE-1F-CA-AE-F9-B1-90-03-4A-5B-1E-AD-51-49-1D-B4-A4-6E-6E-87-3F-7A-AD-4E-CA-CC-4E-37-65-59-62-0E-88-BF-C4-BD-3F-97-F8-51-19-0C-87-04-E5-7E-F7-EB-57-76-5B-7F-B3-FF-00-7D-52-2C-56-C2-51-20-C0-70-31-9D-C6-8E-74-1C-8C-8F-4F-94-2B-C9-0B-72-1C-7E-A2-A9-48-85-19-D0-FF-00-0F-02-B4-D2-2B-74-70-CB-B4-11-D3-E6-A1-A2-B7-77-DE-DB-77-7A-EE-A3-DA-21-72
And here's what I'd get for the second one:

ÿØÿà

The first 4 bytes, the 5th byte is a 00 hex byte. My question is how I can actually get it to save that whole array when converted to the default encoding, or if it already is and just isn't able to be printed to the textbox.

Also, I can't for the life of me figure out how to get the length of a file in bytes. I'd do a do until or a while loop but length is returned as a Long and I can't compare it to an int. In VB I used a do while loop to read 1 byte from the starting point to the end of the file and that worked but it was SO slow, because VB is an interpreted language. C# is much faster, but I can't get it to work like that.

Please help, I'm not asking anyone to write the code for me, just need some help getting on my feet.

Re: BinaryReader in C# acting oddly

Posted: Tue Mar 17, 2009 10:51 am
by LuxuriousMeat
When you pass a byte array as a parameter to 'BitConverter.ToString' it displays the bytes in hexadecimal and uses a '-' as a delimiter. When you use the default encoding, and any text encoding for that matter, it will try to display the readable characters in the file. Your problem has nothing to do with the 'BinaryReader'.