0

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;
}
MSalters
  • 173,980
  • 10
  • 155
  • 350
  • Should be char hexVal[4] instead of hexVal[3], one space for the null terminating character – novice Jan 27 '16 at 05:42
  • http://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c - To get file length in c++ – novice Jan 27 '16 at 05:44
  • @novice - yes, it still has same behavior though. thank for other link too – Nikolai Fedorov Jan 27 '16 at 05:45
  • What does "it's very later" mean? "i would want to change 0xAB, but my code make it change 0XCD". What is the "it" in that sentence and where in your code is that being done (I can't see it)? – kaylum Jan 27 '16 at 05:45
  • I think you better give us some sample *exact* input, expected output and actual output. – kaylum Jan 27 '16 at 05:49
  • You cannot just call `strncmp(&c ...)` because &c is not a null terminated string. Also, strncmp returns 0 on equal. – Hellmar Becker Jan 27 '16 at 05:51
  • @NikolaiFedorov this makes sense: -1 for <, +1 for >, 0 for = – Hellmar Becker Jan 27 '16 at 05:55
  • @HellmarBecker: yes i am beginner, and associated 0 = false, 1 = true, so my mistake. best way to understand maybe why it not works is to try the code and see what it does. thank you again. i try suggestion but still run into same issues... – Nikolai Fedorov Jan 27 '16 at 06:00
  • There are just too many things wrong with your code, read all the comments and answers then rewrite it please – novice Jan 27 '16 at 06:04
  • @NikolaiFedorov - still gives the wrong output, doesnt it – novice Jan 27 '16 at 06:08
  • What would be most helpful to get a better answer is providing sample input, expected output, and actual output as @kaylum suggested. It is unclear what you're trying to do. Do you just want to change one particular number in the input file to another? – Marc Khadpe Jan 27 '16 at 06:10

2 Answers2

1

This is an error.

if(strncmp(&c, hexVal, 2) == 1) {

The first argument to strncmp() is supposed to be a NULL-terminated string. However, you are passing it a pointer to a single character. I don't understand what your convert_to_hex() function is trying to accomplish, otherwise I could suggest an alternative.

To determine the file length, just check if the return value from fgetc() is EOF. EOF is a special value that indicates you're at the end of the file.

int c = fgetc(fp); // declare an int to hold the return value from fgetc()
int fileLength = 0; // keep track of the file length
while(c != EOF) { // repeat while we're not at the end of the file
    c = convert_to_hex(c);
    printf("%02x ", c);
    c = fgetc(fp); // get the next character
    fileLength++; // increment fileLength for each character of the file.
}
// we're done! - fileLength now holds the length of the file
Marc Khadpe
  • 2,012
  • 16
  • 14
1

it turns out the answer was very simple, changing my convert_hex to:

int convert_to_hex(char c)
{
    if (c == 0x69) {
        c = c + 1;
    }
    return c;
}

this answer is what solved it for me. thanks everyone else too.

Community
  • 1
  • 1