Reading a binary file in 68K buffer, and extract/display just bytes 16-20

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BlackLibrary
    New Member
    • Jan 2010
    • 16

    Reading a binary file in 68K buffer, and extract/display just bytes 16-20

    The title probably tells it all.

    I'm a VB6 going to C# and trying to take a task I can do easily in VB6 and do it in C#.NET. I can really use some help and any pointers you can share.

    Below is the code I've created so far to the job. Attached is the binary file in question so you can proof it for yourself.

    Form has a multline textbox and a button event.
    [code=c#]
    private void button1_Click(o bject sender, EventArgs e)
    {
    var openFileDialog = new OpenFileDialog( );
    if (openFileDialog .ShowDialog() == DialogResult.OK )
    {
    string filepath = openFileDialog. FileName;
    // Open the file using the OpenFile method
    var bufferedStream = new BufferedStream(
    openFileDialog. OpenFile());
    Byte[] bytes = new Byte[68];

    //the line below didn't work and so its commented out...
    //but I don't know why it didn't...
    //heroText = bufferedStream. Read(bytes, 16, 11)

    string heroText = Convert.ToBase6 4String(bytes);
    textBox1.Text = heroText;

    }


    }
    [/code]
    Note: Ultimately, I am looking to just extract an exact set of characters in the dat file which happen to be hero names. You'll see them easily if you open the attached file in binary.

    (Yep, its an OLD game file. Online Baked Cookie goes to who guesses it first and helps me with the solution. :) )
    Attached Files
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why read in the whole file? If you are only concerned with bytes 16-20
    Read in 20 bytes and ignore the first 16?
    Your hero name is limited to 4 bytes?

    [code=c#]
    string filename = "";
    //populate filename
    FileStream fs = new FileStream(file name, FileMode.Open);
    byte[] buff=new byte[20];
    int NumRead = fs.Read(buff, 0, buff.length);
    string heroText = Encoding.ASCII. GetString(buff, 16, 4);
    [/code]

    Edit: Ok from the file it looks like you want to start at the 16th index, and read until you come accross the null byte (0x00, which would be the string terminator) so my example code won't give you the full name. Each "hero" entry looks to take up 68bytes (not 68k), and i saw 68 entries, then some stuff at the end

    I don't normally do this much, but this interested me enough to start you off:
    [code=c#]
    string filename = @"c:\soldier.da t";
    //populate filename
    FileStream fs = new FileStream(file name, FileMode.Open);
    byte[] singlehero=new byte[68];
    int NumRead = fs.Read(singleh ero, 0, singlehero.Leng th);
    //name starts at index 16 and goes until there is a 0x00
    int idx = -1;
    for (int i = 16; i < singlehero.Leng th; i++)
    {
    if (singlehero[i] == 0x00)
    {//ending marker
    idx = i - 1-16;//need to subtract out starting point, and 1 for the 0x00
    break;//no need to look further
    }
    }
    if (idx != -1)
    {
    string heroText = Encoding.ASCII. GetString(singl ehero, 16, idx);//your hero name is limited to 4 characters?
    }
    [/code]

    Comment

    • BlackLibrary
      New Member
      • Jan 2010
      • 16

      #3
      First off...thank you for you efforts! You hit it spot on.

      A few followup questions, though, if you (or others) don't mind.

      1. So why didn't heroText = bufferedStream. Read(bytes, 16, 11) work?
      Couldn't I take this and convert it?

      2. Converting...I know that C# is has strong casting compared to VB. But where did I go wrong with the statement below. Did I mix this up? Would the convert statement below convert to string?

      string heroText = Convert.ToBase6 4String(bytes);

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Base64 is a completely different numbering system, not what you are looking for.
        Think binary vs decimal vs hexidecimal vs base64
        binary=base 2
        decimal =base 10
        hexidecimal = base 16
        base64 = base 64 (ta da)

        1. So why didn't heroText = bufferedStream. Read(bytes, 16, 11) work?
        Couldn't I take this and convert it?
        There's a few things wrong with it, .Read() return an integer reflecting the amount of bytes actually read, not a string. The parameters passed to the function aren't what you think either.
        The first parameter is a byte[] used to store the data you are reading from the stream, that part is ok.
        The 2nd parameter is the index you want to start putting bytes at in passed in buffer, not in the stream.
        The 3rd parameter is the maximum number of bytes you want to read. (Thus why it returns an integer of how many it actually read. Example: you could request 100bytes but there is only 89 to read)

        If your plan is to make somewhat of a "character parser", you should probably create your own class that takes a byte[] as a constructor parameter, that way you can parse out the name and any other information from the 68 bytes you want.

        Comment

        • BlackLibrary
          New Member
          • Jan 2010
          • 16

          #5
          Ah...ok.

          Create a class that takes the 68 byte buffer in as the parameter...mak es sense. Ok. Cool.

          I'm so new to C#, that I think I got unnerved and used any command that seemed to even look remotely like it would give me an answer.

          Why on earth did I think that base 64 to string would do for converting a binary to string??? LOL!


          What would you use the .Read for then if it just returns the integer total read?

          In any case...thank you so much!

          Comment

          • BlackLibrary
            New Member
            • Jan 2010
            • 16

            #6
            Whoops...never mind. I see the value of the Read returning the integer.

            Comment

            • RedSon
              Recognized Expert Expert
              • Jan 2007
              • 4980

              #7
              BlackLibrary,

              Be sure you review the documentation about the APIs that you plan on using. Calling a method without knowing what arguments it needs or what the return value is, is poor form.

              Comment

              Working...