Jump to content

Recommended Posts

Ok, the file I'm reading has a header and footer, and in between is the data I need, which is a png image.  In the header there is the length of the png file within 4 hex bytes starting at offset 0x40.  The png starts at offset 0x44.  I need to be able to read in those 4 bytes, get their decimal value so I can know how many bytes there are, then I need to pass that value to readbytes so that it can read that many bytes starting from offset 0x44.

 

Here is an example:

 

@ 0x40, 4 Hex Byte Array: 00 00 A3 E1

That converts to 41953 in decimal

That means that starting at offset 0x44, I need to read 41,953 bytes to get to the end of the data, after that is the footer.

 

But for the love of god I can't do it.  It won't let me convert from a byte array to decimal, and I've tried converting to string, int, etc, it won't let me!?!  I know it's possible, there are programs that do what I'm trying to do (I'm trying to do it so I can learn the language), but HOW?

 

Please please PLEASE someone help me!

To be technical, you should probably use that as an unsigned int.

 

 

But anyway....  A char is simply a byte when talking about single byte chars, so, you can convert a char to another numeric type (yeah, a char is basically just a numeric type....  it's essentially an 8 bit integer).

 

 

So, you can cast a byte to an int.  That doesn't help you since you could do it four times but that leaves you with 4 ints.  That's where bitwise magic comes into play.

 

 

So, let's say you have 4 bytes.... b1, b2, b3, b4.

 

Let's say you concatenate them as a string such as b1b2b3b4 then convert them from hex to decimal....  That's easy enough, but it's also terribly inefficient.

 

What you should realize:

 

Surely by now you've been exposed to binary, but in case not, I shall explain.

 

In base 10, you know for each place you multiply by 10, yes?

 

41953 for example is 4 * 10^4 + 1 * 10^3 + 9*10^2  + 5*10^1 + 3*10^0

 

Well actually I'm not going to explain binary since I don't need to.

 

Now, in hex, you know that.

 

1AB90 (109456 base 10) in decimal is 1*16^4 + 10*16^3 + 11*16^2 + 9*16^1 + 0*16^0

1*16^4 = 65536

10*16^3 = 40960

11*16^2 = 2816

9*16^1 = 144

0*16^0 = 0

Add all those together: 109456

 

 

 

So hopefully you'll understand the logic behind this:

 

 

For a collections of bytes b1, b2, b3, b4, the value of them if they were to be considered 1 chunk of something could be determined by:

 

b4 + (b3 << 8) + (b2 << 16) + (b1 << 24)

 

 

Or, a more concrete example:

 

00 00 A3 E1

b1 b2 b3 b4

 

A3 = 163

E1 = 225

 

225 + (163 << 8)

 

x << y is equivalent to multiplying x by 2 y times since it adds a bit, therefor, x << y = x * 2^y.

 

So, 225 + 163*2^8 = 41953.

 

 

 

I don't know how it would be coded in C#, but I would imagine it's similar to how it would be done in C.

 

In C, it would look something like:

 

byte[] b = {0x00, 0x00, 0xA3, 0xE1};

unsigned int i = b[3] + (b[2] << 8) + (b[1] << 16) + (b[0] << 24);

I got it, I asked for help on another forum that commonly works with these kinds of files.  Turns out the values need to be read in Big Endian, so this:

 

byte[] pnglen = br.ReadBytes(4);

Array.Reverse(pnglen, 0);

int offset = Convert.ToInt32(pnglen);

 

Gave me back the numerical value that was exaactly what I needed.

True dat.

 

Another question, if I might.  I can't figure out how to write the pngbytes byte array to a file.  It contains the bytes from the offset 0x44 through the length aquired from the 4 bytes starting at 0x40.  I don't know how to write it to a file though.  I've got the savefiledialog and all that, I just need to know how to do wpng.Write(?).

Hrmmm, not sure exactly since I have 0 C# experience, but from some brief googling, I've managed to come up with this:

 

//pretend b is a byte array containing what you read from the original file.
FileStream f = new FileStream("out.png", FileMode, FileAccess);
BinaryWriter br = new BinaryWriter(f);
br.Write(b);
br.close();

If you're interested, here is the program:

 

http://infectionist.com/misc/openBLF.exe

 

And a test file that you can open in it:

 

http://infectionist.com/misc/c_005.blf

 

If you open that in a hex editor you'll notice that a JPEG header starts at offset 0x44, well starting at offset 0x40 the 4 bytes there are the size of the file in bytes, in big endian.  This app just gets that value and then reads the bytes from 0x44 to the length specified from 0x40.  The resulting image file will be saved to the same directory the BLF file is in and with the name [original filename].png.  The batch feature doesn't work yet and the program will only open BLF files.

 

Thank you for your help.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.