Recommended Changes:
1) Change the buffer to char (byte), since ftell() will report the size in bytes ( char ) and malloc() also use the byte size.
unsigned int buffer [];
to
unsigned char *buffer;
2) This is normal, the size of bytes and buffer points for bytes, but may be clearly devoid
buffer = malloc(size); /*allocate space on heap*/
to
buffer = (unsigned char *) malloc(size); buffer = malloc(size);
3) change the second parameter from sizeof(unsigned int) to sizeof *buffer , which is 1.
else if (fread(buffer, sizeof(unsigned int), size, fp) != size){
to
else if (fread(buffer, sizeof *buffer, size, fp) != size){
4) Change "%x" to "%02x" otherwise the hexadecimal digits with the hexadecimal digit confuse the output. E. g. "1234" four bytes or two?
printf("%x", buffer[i]);
to
printf("%02x", buffer[i]);
5) Your cleaning at the end of the function may include
fclose(fp); free(buffer);
chux source share