Jump to content

Visual Basic Studio 2008: Opening a file as HEX


Recommended Posts

I'm trying to open a file and extract all characters from a certain offset and save them as a JPG image.  The offset is E000 or D000, it depends on the input file.  For now we'll just use E000 though.

 

My problem is that I can't figure out how to open the file as a Hex string.  I can open it as a string and put it into a textbox, that's no problem, I just don't know how to make it display the hex values of each character.

 

So here's what I need to do: open a provided file through OpenFileDialog and pull out everything from offset E000 to the end of the file, then save that as a .jpg image.

 

How would I go about doing this?

Link to comment
Share on other sites

Disclaimer:  I know nothing of Visual Basic, but I would assume it is done similarly as to how it could be done in C++.

 

 

When you say the hex representation of each character, do you mean as in a char as in 1 byte?  Also, do characters contain anything ASCII, or do they contain numbers?  ASCII representation of a number -> hex has an extra step than binary value -> hex representation.

 

 

Oh, and if you mean 1 byte as a "character", you should represent two characters with 1 hex character.  Hex is base 16, so it would make sense to have 2 bytes (8 bits) in 1 character (16 bits).

 

 

 

If what ever handle is returned is an fopen()-style handle, you could just fseek to the offset and start reading until EOF is reached.

Link to comment
Share on other sites

Stupid me I wrote a reply then closed my browser before I posted it!

 

Ok, so what I need is the...well, idk.  Here, I have a file called offset.txt that contains the text E000, when I open it in a hex editor I get this on one side:

 

45 30 30 30

 

And the actual text is E000.  For the files that I'm trying to open and get that part out, this is the first few bytes of hex:

 

43 4F 4E 20 01 A8 3C 06 EF 8A A7 58 38 30 33 33 39 35 2D 30 30 31

 

And the actual text, which I think is unicode:

 

CON ..<....X803395-001

 

When I open this file and print it's contents using this VB code, this is all I get, meaning it only prints these characters:

 

CON �<X803395-001

 

I need the unicode, I guess.  Basically, open a jpg file in a hex editor (not notepad) and look at the text, that's what I need to get, starting with the 6 characters before the JFIF text.

Link to comment
Share on other sites

It looks like ASCII to me.

 

Only ASCII values 32-126 (in decimal) are typically displayed correctly/uniformly.

 

 

I'm confused as to what you want to extract.

 

 

"...that's what I need to get, starting with the 6 characters before the JFIF text," makes me think you mean some length of content starting from the beginning, but where do you want to stop?  Also, you want to show the data represented as hexidecimal, yes?

 

(Example: JFIF would be 4a 46 49 46.)

Link to comment
Share on other sites

At hex offset E000 in the file there is the start of a JPEG image header.  That header, in Hex, is:

 

FF D8 FF E0 00 10 4A 46 49 46

 

Or as it appears in my hex editor:

 

......JFIF

 

That is what defines the first few characters of a JPEG imag header.  I need to extract everything from E000 to the end of the file and save that as a JPEG.  It's already a JPEG image, so I'm not trying to convert anything, it's just embedded inside another container file.  The code that I need to get out looks like the above hex in hex, the...I'm assuming ASCII, looks like the .....JFIF above, but if I open it in notepad it looks like this:

 

ÿØÿà JFIF

 

I don't know how to better explain, but I get the feeling I'm not doing a good job.  Basically I want everything from E000 to the end so that when I open it in Hex Workshop the hex I see for the header is the above, and when I open it in notepad I see (ÿØÿà JFIF) that.

Link to comment
Share on other sites

I don't know how you would do it in Visual Basic.

 

In C++ I would just do something like:

 

 

FILE* f = fopen("someimage.jpg");

fseek(f, 14, SEEK_SET); //I think JFIF starts at 14.... might be 16 or 12 don't remember.

//read contents

 

 

Or you could just read the entire contents into memory and chop off the first x bytes or what ever.

Link to comment
Share on other sites

No no no, I'm not opening a JPG file, I'm opening a file that has no extension, it's called a CON file, it's used by the Xbox 360 to store data about games.  Gears of War 2 and Halo 3 allow you to take screenshots, and when you do so the screenshot gets put into a CON file in that game's directory under your profile.  The CON fil contains information about you, the console, the game, and the screenshot, then the actual screenshot itself which starts at offset E000, I need to get everything starting from the 6 bytes before the text JFIF (If I chope those bytes off the JPG won't work) and save it as a .jpg file.  It's already in JPG format, it just needs to be separated from the CON file.

Link to comment
Share on other sites

Ahhh I was wondering where the offset E000 came from.

 

 

The process would be the same though.  You would just fseek to the part where the JPG data starts.  If it's always the offset E000, then that's easy.  If not, you'll have to read through the bytes until you come across the values for JFIF.

Link to comment
Share on other sites

Well it's one of 2 offsets, either D000 or E000, shouldn't be too difficult.  However, the code you gave me is for C++, I need VB, or C#.

 

I'm sorry but...on the one hand in shouldn't be too difficult, but on the other you're complaining that someone hasn't spoon-fed you the correct solution? Corbin freely admitted that he had no experience with the language you were looking to use but suggested an approach which was language-independent and then gave an example from a language he knew. Perhaps now you might be able to investigate the equivalent functions in VB to do what you need.

Link to comment
Share on other sites

If you had any idea how many google searches I've done on this, you wouldn't say that.  I AM looking into this on my own.  My dad also did programming in Visual Basic for years, but it's also been years since he's done it.  I downloaded C# and I'm trying to start using it, but I'm having more trouble with it than I am with VB.

Link to comment
Share on other sites

If you get really desperate, you could do it the long way.  (Maybe the only way, but surely there is a more elegant way than what I'm about to suggest.)

 

 

You could declare the FILE structure, fopen, fread, fseek and fclose as external functions and then just use them.

Link to comment
Share on other sites

Now that you've moved to another language/thread there is no point in me posting this, but oh well.

 

 

Technically, when it comes to languages that compile to byte code (in the traditional sense), functions are language independent when compiled (well....  for the most part).

 

 

fopen is just a block of memory in the Windows kernel (I think it's in the kernel.... not sure).

 

So, Visual Basic code can reference that memory and link the application against it.  In other words, fopen can be called from inside of Visual Basic.

 

 

 

It's almost exactly like if you code a DLL or lib in C/C++ or what ever, you can still use it in Visual Basic.

Link to comment
Share on other sites

Robot, if you want a literal answer to your question, they're posts on various different sites about how to open a file in VB.

 

Now my turn for a question, why did you post those?  I wasn't asking how to open a file, I was asking how to read a file as hex starting from a certain offset and ending at the end of the file.

Link to comment
Share on other sites

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.