This problem set is the worst and it made me cry 🤣

Take a look at this code:

#include <stdio.h>
#include <stdlib.h>

#include <string.h>

int main(int argc, char *argv[])
    // check if the card name has written
    if (argc < 2)
        printf("Usage: ./recover image\n");
        return 1;

    // save the card name in a variable called 'card'
    char* card = argv[1];

    // open the card and check if it exist or not
    FILE* inptr = fopen(card, "r");

    if (inptr == NULL)
        printf("Usage: File not exist\n");
        return 2;

    // read the opened file and check if it's a JPGE file
    unsigned char* buffer = malloc(512);
    unsigned int images_count = 0;

    while(fread(buffer, 512, 1, inptr) == 1)
        for (int i = 0; i < 512; i++)
            if (
                    buffer[0] == 0xff &&
                    buffer[1] == 0xd8 &&
                    buffer[2] == 0xff &&
                    (buffer[3] & 0xf0) == 0xe0

    printf("Images: %i\n", images_count);  //output = 26500 ??? Should be 50.


Some of this code I just copied because of the time I've wasted on this Pset and it's so difficult to me, Here are what I don't understand:

  1. If I created a unsigned char* buffer = malloc(512); this means that buffer is now a string right?
  2. If buffer[0] is a string, How I compare buffer[0] == 0xff and not buffer[0] == "0xff" as "0xff" is a string
  3. What is 0xff? I know it's Hexadecimal but I don't understand how it works here.
  4. If you take a look at the discription here you will find that there are 50 images in the card.raw file and my code above tells me 26500!!

1 Answer 1


Let's take your questions in order.

  1. Yes, it's a string, but remember that a char is also a one-byte unsigned int here. In other words, the code is creating a buffer of 512 bytes of just plain raw hexadecimal data. If it were of uint8_t type, it would be the same thing.

  2. The entire buffer would be a string (if we were actually working with strings and not raw data), but buffer[0] is not a string. It is a single unsigned char, also known as a single byte of data. 0xff is a specific notation for hex (see 3). If you put it in double quotes, it would become a 4 char literal string, and not the hex data you're checking against.

  3. 0xff is a form of notation that tells the computer what you are comparing. The 0x tells the computer that what follows is hex data and that what follows is the actual data to read. So, 0xff tells the computer that the data is hex and the value in hex is ff.

  4. The code does the check inside a for loop that cycles 512 times. 512 * 50 = 26500 ! This for loop is just checking the first 4 bytes over and over, 512 times. It appears to me that you're trying to check every byte in the buffer for a signature. It is only necessary to check whether the first 4 bytes are a signature. That means only one check for every 512 byte block!

Resolve that, and you should be back on track in developing the program! Happy programming! ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .