matyyyy
matyyyy

Reputation: 387

Creating copy of binary file from hex representation of it

I'd like to make copy of my binary file, but I need to make it from hex representation of my binary file.

In the first program I create txt file with with hex representation of my binary file:

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

const int BYTE = 1;
int counter = 0;
int read;
long size;
FILE *file1 = NULL; 
FILE *file2 = NULL; 
fpos_t length;

int main() {

        unsigned char hex[3];
        unsigned char buffer[1];
        file1 = fopen("server.pdf", "rb");
        fseek(file1, 0, SEEK_END);
        fgetpos(file1, &length);
        size = length.__pos;
        fseek(file1, 0, SEEK_SET);
        if (file1) {
                file2 = fopen("test.txt", "w");
                while (counter < size) {
                        read = fread(buffer, 1, BYTE, file1);
                        counter += read;
                        i = 0;
                        while(i<read) {
                        sprintf(hex, "%02x", (unsigned int) buffer[i++]);
                        fwrite(hex, 1, BYTE, file2);
                        }
                }
        } else
        printf("ERROR");
        fclose(file1);
        fclose(file2);


}

In the second, I read data from txt file and after that I write it to binary file:

#include <stdio.h>

FILE *file1;
FILE *file2;
int size;
fpos_t length;
int main(){
        file1 = fopen("test.txt", "r");
        fseek(file1, 0, SEEK_END);
        fgetpos(file1, &length);
        size = length.__pos;
        fseek(file1, 0, SEEK_SET);
        char buffer[1];
        char hex[3];
        int counter = 0;
        int read;
        if(file1){
                file2 = fopen("test.pdf", "wb");
                while (counter < size) {
                read = fread(hex, 1, 3, file1);
                counter += read;
                sscanf(hex, "%02x", buffer);
                fwrite(buffer, 1, 1, file2);
                }
        }
        fclose(file1);
        fclose(file2);
}

Unfortunately I can't open my copy. What is the reason?

Upvotes: 0

Views: 1381

Answers (2)

foo
foo

Reputation: 387

Have you looked at the files content? You wont be able to sprintf the hex representation to the variable hex since it's 1 byte in size.

The variable hex is declared hex[BYTE] where BYTE = 1, but your sprintf format string looks like this: "%02x" ie 2 bytes, then you need room for a terminating zero.

The same goes for when you write to the file, you only write 1 byte from your hex string.

Declaring a variable as: var[1] is pointless you can achieve the same thing with var btw.

Besides this you should also add proper error handling, if you can not successfully open the file. This means checking the file pointer after your call to fopen, then take an appropriate action. perror() will print an error string that corresponds to errno, and in case of a file that does not exist it will print something like: "no such file or directory" or similar.

Upvotes: 2

Tio Pepe
Tio Pepe

Reputation: 3089

When you said you can't open your copy, you mean you have an error in fopen("test.txt", "r")? Did you check errno value? Check perror() and strerror(). Besides, you have no loop in second program.

Upvotes: 1

Related Questions