Hex Editing help and information please...

This is a discussion on Hex Editing help and information please... within the C Programming forums, part of the General Programming Boards category; Ive written a short and simple DOS General C program (Dev-C++) to lsit the files in my working directory, then ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    45

    Hex Editing help and information please...

    Ive written a short and simple DOS General C program (Dev-C++) to lsit the files in my working directory, then read and print out the hex of any file name entered (it takes the file entered, reads the actual file).

    Now, the line number is listed in hex perfectly fine, but the hex of the string on that line is the same for every line, and not really in hex.

    Here is my hex printing program:
    Code:
    void FileRead(const char* FilePath) {
         auto char line[200];
         auto FILE *fp;
         int count = 1;
         if((fp = fopen(FilePath, "rb"))==NULL) { printf("%s does not exist!", FilePath);
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, fp) != NULL)
         {  
                                    printf("0x%08x:  %10.8x\n",count,line);
                                    count++;
                                    if((getch())=='\r') 
                                                             break;
         }
    }
    }
    The output puts the line number in hex and the string in the same hex over and over even tho its different.

    Now, once that is fixed, id like to be able to edit that printed data...

    My guess is to feed each of those lines into an array (check) print them out, line by line, making a line identifier to see what line your on (check) maybe make it a 2d array to show the current position in each line? (So a 3d array? is that possible? hexFile[totalLines[Currentline][posInLine?])

    Could someone just give me a code snippet for editing hex in a file? I really really dont want to spend a week asking and taking forever to finally find an example to learn from.
    Last edited by SG57; 06-24-2006 at 02:02 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %x is to print an integer, not a string. Try looping through each character in the string and printing it.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > but the hex of the string on that line is the same for every line
    What you're printing is the hex address of where the string is stored - which is always in the same place as far as this bit of code is concerned.

    > auto FILE *fp;
    Wow, it's a long time since anyone bothered with the auto keyword.
    How old is your book?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    thx for the hint, i have it working printing hex (kinda, it messed up a little, like combines line 3 with line 1.. and prints out line 1 holding all the data) but trying to write hex doesnt work, it just simply doesnt write anything... here is my write and read code.

    Oh and I just was testing around with keywords in C to see what happens. Is auto bad to use nowadays? I know it basically means 'local' variable, it can only be called in a function and that only that function its called in can use it.
    Code:
    void FileReadPrintHex(const char* FilePath) {
         char line[200];
         FILE *fp;
         int z=0;
         int count = 1;
         if((fp = fopen(FilePath, "rb"))==NULL) { printf("%s does not exist!", FilePath);
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, fp) != NULL)
         {  
                                    printf("\nLine %i: ",count);
                                    while(line[z]!='\n') {
                                                           printf(" %02x",line[z]);
                                                           z++;
                                    }
                                    count++;
         }
         fclose(fp);
    }
    }
    void FileWriteHex(const char* FilePath, char line[200]) {
         FILE *zp;
         int z=0;
         int count = 1;
         if((zp = fopen(FilePath, "wb"))==NULL) { printf("%s does not exist!", FilePath);
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, zp) != NULL)
         {  
                                    while(line[z]!='\n') {
                                                           fprintf(zp,"%02x",line[z]);
                                                           printf("%02x",line[z]);
                                                           z++;
                                    }
                                    count++;
         }
         printf("DONE!");
         fclose(zp);
    }
    }
    Anything im doing wrong? it combines the hex together tinto line 1 (when printing, not writing). When writing, it doesnt write atall, it just formats the file to be written to.

    Thx

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to reset z to zero before printing the line in hex.
    Code:
       while ( fgets(line, sizeof line, fp) != NULL) {  
              printf("\nLine %i: ",count);
              z = 0;
              while(line[z]!='\n') {
                   printf(" %02x",line[z]);
                   z++;
              }
              count++;
         }
    Kurt

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Anything im doing wrong?
    Probably not resetting z back to 0 after the first line.

    Code:
    while ( fgets(line, sizeof line, zp) != NULL) {
      displayLine( line );
    }
    You have essentially the same code in two places, so writing a function to do that specific task allows you to re-use.

    > Is auto bad to use nowadays?
    More like pointless.
    It can only be used in one place (where you used it), and the meaning of the variable is exactly the same whether you say 'auto' or not.
    Other qualifiers like static, volatile and register can all change the meaning, but auto does nothing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    oh, ok thx alot.

    Its just I noticed when googling that 'auto' declares things as local in a function. So I thought why not test it out. I wont use it again though.

    Ok, the printing of hex is perfect now. Turned out while(line[z]!='\n') had to be while(line[z]!='\r') because I did press return.

    Anyway, now my writing of hex is off... it doesnt write anything, it just formats it still...
    Code:
    void FileWriteHex(const char* FilePath) {
         FILE *fp;
         char line[200];
         int z=0;
         int count = 1;
         if((fp = fopen(FilePath, "rb"))==NULL) { 
                printf("%s does not exist!", FilePath);
                sleep(1000);
                main();
         } else {
         printf("\n\nNow Opening: %s\n", FilePath);
         while ( fgets(line, sizeof line, fp) != NULL)
         {  
                                    printf("\nLine %i: ",count);
                                    z=0;
                                     scanf("%s",line);
                                    while(line[z]!='\r') {
                                                           fprintf(fp,"%02x",line[z]);
                                                           z++;
                                    }
                                    count++;
         }
         fclose(fp);
    }
    }
    Anything wrong here?
    Last edited by SG57; 06-24-2006 at 08:01 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Apologies for not following this thread closely, even now. FWIW, from what I did gather it sounds similar to this. HTH.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    45
    Hey thx, that helps alot.

    Now i need to set a line in the array, and be able to edit it...

    Ill come back here if i run into any problems.

    Or if anyone has a hint for me to do such a thing, thatd be great also!

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    > Turned out while(line[z]!='\n') had to be while(line[z]!='\r')
    It would have been '\n' (and always would be \n) if you opened the text file in "r" mode and not "rb" mode.
    The "b" mode turns OFF the OS line ending to ANSI-C line ending translations.

    Or if this is really a binary file, then you should be using fread() to read blocks of data. fgets() will not handle \0 for example in any meaningful way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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