Thread: Reading Hex Values From A File

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    40

    Reading Hex Values From A File

    So I'm working on a university project and I need to read some data from a hex file. I'm working on linux and when I use xxd in the terminal to read the file I get this output

    Code:
    00000000: 1e 5e c3 1e 40
    The problem is when I print out the file with the bellow code there are a bunch of "ffffff" characters in the file. (also bellow). Are these like new lines or something? I have no idea why they are there.

    Here is the code:
    Code:
    char c;
        while (!feof(in)) {
          c = fgetc(in);
          printf("\n%02x", c);
        }
        printf("\nEnd of file\n");
    Here is the output:

    Code:
    1e
    5e
    ffffffc3
    1e
    40
    ffffffff
    End of file

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    That's because you end up printing what EOF is, which is typically -1

    Your while loop should be
    while ( (c = fgetc(in)) != EOF )

    Further, when you call printf, you should cast c to unsigned int.
    On your system at least, char is signed, which means it gets sign extended when c is in the range 0x80 to 0xFF
    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.

  3. #3
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Thanks for the reply.
    I switch to a unsigned char and it fixed the problem... cheers!!!

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Ok so now I'm printing out 6 values instead of 5. Im getting a extra "FF" at the end. Bear with me here please I'm still new to programming. Is my loop printing out EOF like Salem said? I tried using his code for the condition of the while loop but that just seems to endlessly print out "FF"??

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I suggest you post your latest code.

    Perhaps you missed out a pair of ()
    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.

  6. #6
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    here is my code now...

    Code:
        unsigned char c;
        while (!feof(in)) {
          c = fgetc(in);
    
    
          printf("%02x\n", c);
        }
        printf("\nEnd of file\n");
    All I have done is change the char to unsigned.

    Its printing this now... the ff at the end shouldn't be there.

    1e
    5e
    c3
    1e
    40
    ff
    Last edited by Xecutive; 11-12-2016 at 05:01 PM.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    So in other words, you carried on using feof() incorrectly and ignored my corrected while loop.
    FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com

    Also, c must be declared as an int, and only AFTER reading it should it be cast to an unsigned char for other purposes.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    40
    Thanks for the help Salem. I had a read of the FAQ and saw your suggestion shown on there. If you go back to my second from last post you will see I said that I said I tried to change the condition of the while loop just like you asked. For some reason though it seems to just endlessly print out "FF". I have posed the change here just encase I made a stupid mistake somewhere. I'm also now reading the characters into a int like you said and then casting to a char.

    Code:
        unsigned char c;
        int a = 0;
        while ( ( (c = fgetc(in)) != EOF )) {
          a = fgetc(in);
          c = a;
         
          //int opcode = c & 0xc0;
          //opcode = opcode >> 6;
    
    
          //int operand = c & 0x3f;
    
    
          //printf("\n%d %d", opcode, operand);
          
          printf("%02x\n", c);
        }
        printf("\nEnd of file\n");
    Last edited by Xecutive; 11-13-2016 at 07:22 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Like I said, c MUST be an int.

    Code:
    int c;
    while ( ( (c = fgetc(in)) != EOF )) {
            printf("%02x\n", (unsigned)c);
    }
    fgetc() returns every possible value of char, and EOF.
    In an 8-bit char, this means it has to return 257 possible values - hence it cannot itself return a char.

    By making it a char, you implicitly cast what was the real EOF into some char, and at the same time make the != EOF test fail.
    That's because (char)EOF != EOF
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading Values from .txt file and it hangs.
    By btsp in forum C Programming
    Replies: 3
    Last Post: 10-06-2013, 04:37 PM
  2. Reading two values from a file as one value?
    By Flotonic in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 05:53 PM
  3. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 07:15 PM
  4. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  5. Reading float values from a file??
    By neil_w_84 in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2005, 08:48 AM

Tags for this Thread