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. -
private void button1_Click(object 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.ToBase64String(bytes);
-
textBox1.Text = heroText;
-
-
}
-
-
-
}
-
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. :) )
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? -
string filename = "";
-
//populate filename
-
FileStream fs = new FileStream(filename, FileMode.Open);
-
byte[] buff=new byte[20];
-
int NumRead = fs.Read(buff, 0, buff.length);
-
string heroText = Encoding.ASCII.GetString(buff, 16, 4);
-
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: -
string filename = @"c:\soldier.dat";
-
//populate filename
-
FileStream fs = new FileStream(filename, FileMode.Open);
-
byte[] singlehero=new byte[68];
-
int NumRead = fs.Read(singlehero, 0, singlehero.Length);
-
//name starts at index 16 and goes until there is a 0x00
-
int idx = -1;
-
for (int i = 16; i < singlehero.Length; 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(singlehero, 16, idx);//your hero name is limited to 4 characters?
-
}
-
6 3673
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? -
string filename = "";
-
//populate filename
-
FileStream fs = new FileStream(filename, FileMode.Open);
-
byte[] buff=new byte[20];
-
int NumRead = fs.Read(buff, 0, buff.length);
-
string heroText = Encoding.ASCII.GetString(buff, 16, 4);
-
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: -
string filename = @"c:\soldier.dat";
-
//populate filename
-
FileStream fs = new FileStream(filename, FileMode.Open);
-
byte[] singlehero=new byte[68];
-
int NumRead = fs.Read(singlehero, 0, singlehero.Length);
-
//name starts at index 16 and goes until there is a 0x00
-
int idx = -1;
-
for (int i = 16; i < singlehero.Length; 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(singlehero, 16, idx);//your hero name is limited to 4 characters?
-
}
-
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);
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.
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!
Whoops...never mind. I see the value of the Read returning the integer.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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:...
|
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...
|
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...
|
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");
|
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...
|
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?...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
| | |