1

I try to read binary(executeable) files, butsome problem as it prints extra character(0xffffff) after every few bytes. But this code work perfectly for text file(Non-binary). The code is:

int main(int argc, char **argv) {
  uint64_t i=0;
  FILE * fp=fopen(argv[1],"rb");
  uint8_t a;
  long int size=0;
  char *msg;
  size=findSize(argv[1]);
  msg=(char *)malloc(size+1);

  while((a=fgetc(fp))!=EOF)
  {
    printf("%02x",a);
      msg[i]=(char)a;
    i++;
    if(i==size)
      break;
  }
  i=0;
  for(i=0;i<size;i++)
  {
    printf("%x",msg[i]);

  }
    return 0;
}

When i try to print the value(s) of a, It works perfectly, while printing msg[i], it prints extra bytes i,e:

Orginal bytes: 0xa31c4b1ab66b900

Output with extra bytes: 0xa31c4ffffffb1ffffffafffffffb66ffffffb900

superstack
  • 91
  • 5

1 Answers1

2
char *msg;

Your msg variable has datatype char, which is probably signed on your platform. That means, it represents values from -128 to +127.

printf("%x",msg[i]);

Any arguments to printf() that have an integer datatype smaller than int will be expanded to int. That conversion will keep the arithmetic value, so -128 which is 0x80 in char, will expand to 0xFFFFFF80 for int.

To avoid this, you need to store or cast the values to unsigned char first. The uint8_t type is same as unsigned char and easier to read, so:

printf("%x", (uint8_t)msg[i]);

That way the numeric value will range from 0 to 255, like you would expect, and print as 0x00 to 0xFF even after the implicit conversion to int.

jpa
  • 10,351
  • 1
  • 28
  • 45