473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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.
Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             var openFileDialog = new OpenFileDialog();
  4.             if (openFileDialog.ShowDialog() == DialogResult.OK)
  5.             {
  6.                 string filepath = openFileDialog.FileName;
  7.                 // Open the file using the OpenFile method
  8.                 var bufferedStream = new BufferedStream(
  9.                      openFileDialog.OpenFile());
  10.                 Byte[] bytes = new Byte[68];
  11.  
  12.                 //the line below didn't work and so its commented out...
  13.                 //but I don't know why it didn't...
  14.                 //heroText = bufferedStream.Read(bytes, 16, 11) 
  15.  
  16.                 string heroText = Convert.ToBase64String(bytes);
  17.                 textBox1.Text = heroText;
  18.  
  19.             }
  20.  
  21.  
  22.         }
  23.  
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
File Type: txt SOLDIER-DAT.txt (16.6 KB, 438 views)
Jan 5 '10 #1

✓ answered by Plater

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?

Expand|Select|Wrap|Line Numbers
  1. string filename = "";
  2. //populate filename
  3. FileStream fs = new FileStream(filename, FileMode.Open);
  4. byte[] buff=new byte[20];
  5. int NumRead = fs.Read(buff, 0, buff.length);
  6. string heroText = Encoding.ASCII.GetString(buff, 16, 4);
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. string filename = @"c:\soldier.dat";
  2. //populate filename
  3. FileStream fs = new FileStream(filename, FileMode.Open);
  4. byte[] singlehero=new byte[68];
  5. int NumRead = fs.Read(singlehero, 0, singlehero.Length);
  6. //name starts at index 16 and goes until there is a 0x00
  7. int idx = -1;
  8. for (int i = 16; i < singlehero.Length; i++)
  9. {
  10.     if (singlehero[i] == 0x00)
  11.     {//ending marker
  12.         idx = i - 1-16;//need to subtract out starting point, and 1 for the 0x00
  13.         break;//no need to look further
  14.     }
  15. }
  16. if (idx != -1)
  17. {
  18.     string heroText = Encoding.ASCII.GetString(singlehero, 16, idx);//your hero name is limited to 4 characters?
  19. }
  20.  

6 3673
Plater
7,872 Expert 4TB
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?

Expand|Select|Wrap|Line Numbers
  1. string filename = "";
  2. //populate filename
  3. FileStream fs = new FileStream(filename, FileMode.Open);
  4. byte[] buff=new byte[20];
  5. int NumRead = fs.Read(buff, 0, buff.length);
  6. string heroText = Encoding.ASCII.GetString(buff, 16, 4);
  7.  
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:
Expand|Select|Wrap|Line Numbers
  1. string filename = @"c:\soldier.dat";
  2. //populate filename
  3. FileStream fs = new FileStream(filename, FileMode.Open);
  4. byte[] singlehero=new byte[68];
  5. int NumRead = fs.Read(singlehero, 0, singlehero.Length);
  6. //name starts at index 16 and goes until there is a 0x00
  7. int idx = -1;
  8. for (int i = 16; i < singlehero.Length; i++)
  9. {
  10.     if (singlehero[i] == 0x00)
  11.     {//ending marker
  12.         idx = i - 1-16;//need to subtract out starting point, and 1 for the 0x00
  13.         break;//no need to look further
  14.     }
  15. }
  16. if (idx != -1)
  17. {
  18.     string heroText = Encoding.ASCII.GetString(singlehero, 16, idx);//your hero name is limited to 4 characters?
  19. }
  20.  
Jan 6 '10 #2
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.ToBase64String(bytes);
Jan 6 '10 #3
Plater
7,872 Expert 4TB
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.
Jan 6 '10 #4
Ah...ok.

Create a class that takes the 68 byte buffer in as the parameter...makes 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!
Jan 6 '10 #5
Whoops...never mind. I see the value of the Read returning the integer.
Jan 6 '10 #6
RedSon
5,000 Expert 4TB
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.
Jan 6 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Leandro Pardini | last post by:
Hello there, I'm trying to process a binary file and I really don't know how. The story: gPhoto2 downloads the images from my camera just fine, but small areas of 10x3 pixels are screwed up. I...
1
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer...
20
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
20
by: cylin | last post by:
Dear all, I open a binary file and want to write 0x00040700 to this file. how can I set write buffer? --------------------------------------------------- typedef unsigned char UCHAR; int...
8
by: junk5 | last post by:
Hi I need to read raw 16 bit data from a file, where the first byte is the most significant byte of the first data value and the second byte is the least significant byte of the first data value...
8
by: Shalaka Joshi | last post by:
Hi, I have binary file say, "test.bin". I write "FF" in the file and expect my code to read 255 for me. char * lbuf; int lreadBytes ; long lData; FILE *pFile = fopen ("c:\\testbin","rb");
9
by: szclark | last post by:
Hello, I am trying to extract information from a file I have so that I can answer some questions on it, the problem I'm having is that it is returning back mostly gibberish and I really don't know...
3
by: Willy Stevens | last post by:
Hello, In my application I have to read sometimes quite big chunk of binary data. I have a buffer which default size is 32000 bytes. But how could I read binary data that exceeds 32000 bytes?...
0
by: Craftkiller | last post by:
=========Program compiles on both windows and linux=========== Greetings, I am currently working on an encrpytion program that will take a file and a password string and flip the bits based on the...
6
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.