Conversion from binary file to hex in C

匿名 (未验证) 提交于 2019-12-03 10:03:01

问题:

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.

回答1:

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:

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



回答3:

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).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!