I have a few issues with my code which I'm not sure to understand. I read in a file (you can try with any file) to get hex values. I try to find certain hex values and change them - which works sort of, but it's very later than it should be. Example would be:
0xAA 0xAB 0xAC 0xAD 0XAE ... 0XCD 0xCE
I would want to change 0xAB, but my code changes 0XCD. not sure why this is happening, but maybe I'm doing it wrong way. also is there a way to get the file length automatically? I just put a buffer that is part of the file, but I would like to get real length.
#include <stdio.h>
#include <string.h>
#define FLEN 512
int convert_to_hex(char c);
int main(int argc, char *argv[]) {
char c;
int i = 0;
FILE *fp = fopen(argv[1],"rb");
for(i = 0; i < FLEN; i++) {
c = convert_to_hex(fgetc(fp));
printf("%02x ", c);
}
printf("\n");
}
int convert_to_hex(char c)
{
char hexVal[3];
sprintf(hexVal, "%02X", 0x69);
if(strncmp(&c, hexVal, 2) == 1) {
printf(">> %s ", hexVal); // indicate where it change (late)
return c + 1;
}
return c;
}