1

I am trying to write some simple program to uploading files to my server. I' d like to convert binary files to hex. I have written something, but it does not work properly.

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

static int bufferSize = 1024;
FILE *source;
FILE *dest;

int n;
int counter;

int main() {
    unsigned char buffer[bufferSize];
    source = fopen("server.pdf", "rb");
    if (source) {
        dest = fopen("file_test", "wb");

        while (!feof(source)) {
            n = fread(buffer, 1, bufferSize, source);
            counter += n;
            strtol(buffer, NULL, 2);
            fwrite(buffer, 1, n, dest);
        }
    }
    else {
        printf("Error");
    }
    fclose(source);
    fclose(dest);
}

I use strtol to convert binary do hex. After invoking this code I have still strange characters in my file_test file.

I' d like to upload a file on server, for example a PDF file. But firstly I have to write a program, that will convert this file to a hex file. I'd like that the length of a line in hex file would be equal 1024. After that, I will upload this file line by line with PL/SQL.

4
  • What is it you want to have? Can't you just read the binary values with a hex editor? I wouldn't see a sense in converting binary to human-readable hex.
    – guitarflow
    Commented Feb 15, 2012 at 12:00
  • 1
    strtol() doesn't do what you think it does. Read your documentation. Commented Feb 15, 2012 at 12:01
  • How about using base64 format? here is discussion stackoverflow.com/questions/342409/…
    – 2r2w
    Commented Feb 15, 2012 at 12:05
  • @2r4w -- base-64 might have some theoretical or tangential bearing on the OP's confusions, but is of no practical help in the context of his question. Commented Feb 15, 2012 at 14:27

3 Answers 3

1

EDIT: I completely misunderstood what the OP was aiming for. He wants to convert his pdf file to its hex representation, as I see now, because he wants to put that file in a text blob field in some database table. I still claim the exercise is a complete waste of time,since blobs can contain binary data: that's what blobs were invented for. Blob means binary large object.

You said: "I' d like to upload file on server, for example pdf file. But firstly I have to write a program, that will convert this file to hex file."

You don't have to, and must not, write any such conversion program.

You have to first understand and internalize the idea that hex notation is only an easy-to-read representation of binary. If you think, as you seem to, that you have to "convert" a pdf file to hex, then you are mistaken. A pdf file is a binary file is a binary file. You don't "convert" anything, not unless you want to change the binary!

You must abandon, delete, discard, defenestrate, forget about, and expunge your notion of "converting" any binary file to anything else. See, hex exists only as a human-readable presentation format for binary, each hex digit representing four contiguous binary digits.

To put it another way: hex representation is for human consumption only, unsuitable (almost always) for program use.

For an example: suppose your pdf file holds a four-bit string "1100," whose human-readable hex representation can be 'C'. When you "convert" that 1100 to hex the way you want to do it, you replace it by the ASCII character 'C', whose decimal value is 67. You can see right away that's not what you want to do and you immediately see also that it's not even possible: the decimal value 67 needs seven bits and won't fit in your four bits of "1100".

HTH

4
  • Hi, My task is to speedup uploading files to server. In our solution somebody wrote a program which uploads hex representation of file into database as a blob. Unfortunately the each line of file which was created by the program, has only 256B. I' d like to that line would have 1024B. To read the file from database we use inverse program, which get binary representation from hex file.
    – matyyyy
    Commented Feb 15, 2012 at 14:45
  • So should I write something like hexdump?
    – matyyyy
    Commented Feb 15, 2012 at 15:31
  • 1
    Oh, now I understand. You have a bug in your code and you would like someone to find it for you. One bug is this: your approach to binary-to-hex conversion is wrong. You must read each four-bit chunk from the pdf file, starting at the beginning; convert that four-bit chunk to its hex-character representation; store the hex character in your output buffer; and continue for all four-bit nybbles in the pdf. I don't know what you are trying to accomplish with strtoul( ), but you won't. @unwind gave you an answer that you should study to get some understanding of a solution. Commented Feb 15, 2012 at 20:07
  • @user987903 I'm confused as well, how would going through this conversion back and forth "speedup uploading files to server"? As Pete says here, just stick the binary in your blob (that didn't come out right). Maybe I'm missing the point tho
    – tbone
    Commented Feb 16, 2012 at 15:57
0

Your code is fantastically confused.

It's reading in the data, then doing a strtol() call on it, with a base of 2, and then ignoring the return value. What's the point in that?

To convert the first loaded byte of data to hexadecimal string, you should probably use something like:

char hex[8];

sprintf(hex, "%02x", (unsigned int) buffer[0] & 0xff);

Then write hex to the output file. You need to do this for all bytes loaded, of course, not just buffer[0].

Also, as a minor point, you can't call feof() before you've tried reading the file. It's better to not use feof() and instead check the return value of fread() to detect when it fails.

2
  • I' d like to upload file on server, for example pdf file. But firstly I have to write a program, that will convert this file to hex file. I' d like that the length of line in hex file would be equal 1024. After that I will upload this file line by line with plsql. I' m sorry but I am not very experienced with C.
    – matyyyy
    Commented Feb 15, 2012 at 12:46
  • for (i=0;i<bufferSize;i++){ sprintf(bufferHex, "%02x", (unsigned int) buffer[i] & 0xff); } fwrite(bufferHex, 1, n, dest); Is it Ok? }
    – matyyyy
    Commented Feb 15, 2012 at 12:48
0

strtol converts a string containing a decimal representation of a number to the binary number if i am not mistaken. You probably want to convert something like a binary OK to 4F 4B... To do that you can use for example sprintf(aString, "%x", aChar).

Not the answer you're looking for? Browse other questions tagged or ask your own question.