Hex Editing - Remove first 15 bytes

This is a discussion on Hex Editing - Remove first 15 bytes within the C++ Programming forums, part of the General Programming Boards category; What I'm trying to do is, have the user input a file name and it must have the extension .SST. ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    11

    Hex Editing - Remove first 15 bytes

    What I'm trying to do is, have the user input a file name and it must have the extension .SST. Once that is done, it will remove the first 15 bytes of the file and rename it accordingly.

    The file will be renamed to .DDS if the header starts with DDS, or [4444 53] in hex. Else, it will be renamed .TGA

    I'm a beginner in C++, whereas Visual Basic is where I am more knowledgeable. However, doing this operation in VB is not my cup of tea. I saw some code which did hex operations and it seemed pretty short. So I'm attempting to do it with C++.

    If someone could provide this piece of code, I would be very helpful.

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    We'll help, but...

    If someone could provide this piece of code
    Please read the board guidelines and hints, because although we are here 'cause we want to help you program, you are unlikely to get some to write your code for you.

    You should be able to get started from the info in the
    tutorials.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Alright, no code. But hints or links?

    The tutorial is a good help. However, it does not explain writing. I found this: Newsgroup post. Any ideas?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Code:
    ifstream texture;
    string name;
    
    cout << "Enter the filename of the texture you will be converting: ";
    cin >> name;
    
    texture.open (name);
    if (texture.is_open())
    {
    	while (texture.good())
    		cout << (char) texture.get();
    	texture.close();
    }
    else
    {
    	cout << "File not found.";
    }
    That reads the entire file.

    Now, how would I get the user input? texture.open (name); does not compile, however, texture.open ("test.txt"); does. How would I read 15 bytes and check it? Finally, would I be using memcpy put it in the new file?

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Can someone one tell me what I'm doing wrong?
    With this code, it reads from from the 16th byte, which is good. It outputs correctly. But, once it gets to the end of the file, I get an error:

    Invalid allocation size: 4294967295 bytes

    However, thanks for the help guys. It works fine, with the exception of the crappy error (which does not mess up the process it seems).
    Code:
    	ifstream textureRead;
    	ofstream textureWrite;
    	string nameTextureRead;
    	string nameTextureWrite;
    	int convertLoop = 0;
    	int askLoop = 0;
    	int length;
    	char * buffer;
    
    	while (convertLoop == 0)
    	{
    		cout << "\nEnter the filename of the texture you will be converting: " << endl << "e.g.: nav_xbattleship_07t.sst" << endl;
    		cin >> nameTextureRead;
    
    		cout << "\nEnter the filename of the converted texture: " << endl << "e.g.: nav_xbattleship_07t.dds" << endl;
    		cin >> nameTextureWrite;
    
    		textureRead.open (nameTextureRead.c_str(), ios_base::binary);
    		while (textureRead.good())
    		{
    			textureRead.seekg (-14, ios::end);
    			length = textureRead.tellg();
    			textureRead.seekg (15, ios::beg);
    			buffer = new char [length];
    			textureRead.read (buffer,length);
    			textureRead.close();
    		}
    
    		textureWrite.open (nameTextureWrite.c_str(), ofstream::binary | ofstream::out);
    		textureWrite.write (buffer,length);
    		textureWrite.close();
    
    		askLoop = 0;
    
    		while (askLoop == 0)
    		{
    			string askAgain;
    			cout << "\nConvert another texture [y/n]?" << endl;
    			cin >> askAgain;
    
    			if (askAgain == "y")
    			{
    				askLoop++;
    			}
    			else if (askAgain == "n")
    			{
    				askLoop++;
    				convertLoop++;
    
    				cout << endl;
    			}
    			else
    			{
    				cout << "Input not understood. Accepted answers are: y, n" << endl;
    			}
    		}
    	}
    EDIT: Also, how do I clear the buffer?
    Last edited by xTrinity; 08-13-2003 at 01:29 PM.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Lots of problems:
    Code:
    -seekg(-14, ios::end) tells it to seek +14 past the end of file.
    -length = textureRead.tellg();
         -tellg() returns an error code, probably -1 -> 0xFFFFFFFF
    -buffer = new char [length];
         -You're creating a (4gb - 1byte) buffer!
    -textureRead.close();
         -Uh oh, you just closed the file. textureRead.good() != true
         -thus, it will only work if it reads it all in in 1 go, which you're trying to do, but then you don't need the while.
    -You didn't delete[] buffer! memory leak of 4gb right away!


    I believe this would be a better solution all around:
    Code:
    char buffer[256];
    textureRead.seekg(15);
    while (textureRead.good())
    {
    	textureRead.read(buffer, 256);
    	textureWrite.write(buffer, textureRead.gcount());
    }
    textureRead.close();
    textureWrite.close();
    //buffer isn't dynamically allocated so you don't need to delete[] it
    Hope this helps

    **EDIT**
    Clear the buffer in which way? To initialize it to all 0's, do:
    char buffer[256] = {0};
    To free it, do:
    char* buffer = new char[256];
    delete[] buffer; //Only because buffer is dynamically allocated
    Last edited by Hunter2; 08-13-2003 at 02:04 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Hey, thanks for the help. Your way is much shorter .

    However, you forgot to open the new file:
    textureWrite.open (nameTextureWrite.c_str(), ofstream::binary | ofstream:ut);

    Also, since the files I am converting are way over 256 bytes, I had to change:
    textureRead.read(buffer, 800000);
    char buffer[800000];

    Would that work?

    BTW, with this method, I cannot "Convert another". Once I select it to convert another, it does not make the file. I'm suspecting something is wrong. If you guys want to full source, I can post it.
    Last edited by xTrinity; 08-13-2003 at 02:27 PM.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    No, my method creates a 256 byte buffer and reads in 256 at a time, and then writes those 256, and re-uses the buffer for the next 256 bytes, and so on. gcount() tells you how many characters were read, so you won't get garbage characters written in the last 256-byte packet if the filesize isn't a perfect multiple of 256.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Hrmmm.. then how do i make it (with your 256byte buffer) write the full 700Kbs in the new file? It only write 256 bytes.

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Nope, look again My read and write are inside the while loop. So until the end of file is reached, it will keep reading another 256 bytes and writing them to the other file.

    However, you forgot to open the new file
    I thought you could figure that much out for yourself, which you did
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    Hey, thanks! Didn't work before because I put the Write open inside the while. Figures . Took me a few hours last night just to realize the while loop didnt have opening and closing brackets.

    However, I still cant use the "Convert another". But thats justs a little bug.

  12. #12
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah, I believe your problem may be that readTexture still has the fail bit set from eof(end of file). To remedy this, call readTexture.clear() before you open it. And also, I just noticed that you don't close readTexture after you're done with it either. Make sure you do that too

    Another general suggestion, It doesn't really matter too much but... askLoop and convertLoop are both int's, and when you want to break the loops, you go askLoop++ and convertLoop++. Since this only gets called once per decade or so, it's ok but it would probably be better to either do:
    ++askLoop;
    or better yet, just
    askLoop = 1;

    The difference is 0.000000001 ms in processing time (theoretically) because ++askLoop and askLoop++ have to do both addition and assignment, while askLoop=1 just has to do assignment; and askLoop++ has to use a temporary variable in between. But more importantly (here comes the personal preference part), I personally think it's more readable when you do askLoop = 1.

    Ok, I'm done my rant Have fun with your converting!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    11
    The difference is 0.000000001 ms in processing time


    Anyways textureRead.clear() works great. Thanks!!

    0.000000001 ms .....

  14. #14
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Yes, I'm very picky about some things But you know, most programmers will sacrifice the 0.000001 ms in favour of code readability. Since you can have both, why not?

    I'm glad clear() works for you. That particular trick took me half a day to figure out
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 03:15 PM
  2. a large hex counter
    By Peaches in forum C Programming
    Replies: 2
    Last Post: 05-25-2003, 11:16 AM
  3. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 02:54 PM
  4. Help with code(Binary to Hex)
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 05-28-2002, 09:21 PM
  5. Looking to extract a hex value out of a buffer
    By MMC in forum C Programming
    Replies: 9
    Last Post: 04-12-2002, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21