C语言中二进制文件到十六进制文件的转换

发布于 2025-01-06 04:05:48 字数 1089 浏览 0 评论 0 原文

我正在尝试编写一些简单的程序来将文件上传到我的服务器。我想将二进制文件转换为十六进制。我写了一些东西,但它不能正常工作。

#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);
}

我使用 strtol 将二进制转换为十六进制。调用此代码后,我的 file_test 文件中仍然有奇怪的字符。

我想在服务器上上传一个文件,例如 PDF 文件。但首先我必须编写一个程序,将该文件转换为十六进制文件。我希望十六进制文件中的一行长度等于 1024。之后,我将使用 PL/SQL

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮云落日 2025-01-13 04:05:48

编辑:我完全误解了OP的目标。正如我现在所看到的,他想要将 pdf 文件转换为其十六进制表示形式,因为他想要将该文件放入某个数据库表的文本 blob 字段中。我仍然声称这个练习完全是浪费时间,因为 blob 可以包含二进制数据:这就是 blob 的发明目的。 Blob表示二进制大对象。

你说:“我想在服务器上上传文件,例如pdf文件。但首先我必须编写一个程序,将该文件转换为十六进制文件。”

您不必而且不得编写任何此类转换程序。

您必须首先理解并内化这样的想法:十六进制表示法只是一种易于阅读的二进制表示。如果您认为必须将 pdf 文件“转换”为十六进制,那么您就错了。 pdf 文件是二进制文件是二进制文件。您不会“转换”任何内容,除非您想更改二进制文件!

您必须放弃、删除、丢弃、扔窗外、忘记并消除将任何二进制文件“转换”为其他任何文件的想法。请注意,十六进制作为人类可读的二进制表示格式而存在,每个十六进制数字代表四个连续的二进制数字。

换句话说:十六进制表示仅供人类使用,不适合(几乎总是)程序使用。

举个例子:假设您的 pdf 文件包含一个四位字符串“1100”,其人类可读的十六进制表示可以是“C”。当您按照您想要的方式将 1100“转换”为十六进制时,您可以将其替换为 ASCII 字符“C”,其十进制值为 67。您可以立即看到这不是 你想要做什么,但你立即发现它甚至是不可能的:十进制值 67 需要七位,并且不适合你的四位“1100”。

华泰

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

无语# 2025-01-13 04:05:48

你的代码非常混乱。

它读取数据,然后以 2 为基数对其进行 strtol() 调用,然后忽略返回值。这有什么意义呢?

要将第一个加载的数据字节转换为十六进制字符串,您可能应该使用类似以下内容的方法:

char hex[8];

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

然后将 hex 写入输出文件。当然,您需要对加载的所有字节执行此操作,而不仅仅是 buffer[0]

另外,作为一个小问题,在尝试读取文件之前不能调用 feof()。最好不要使用 feof(),而是检查 fread()返回值来检测它何时失败。

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.

执手闯天涯 2025-01-13 04:05:48

如果我没有记错的话,strtol 将包含数字十进制表示形式的字符串转换为二进制数。您可能想要将二进制 OK 之类的内容转换为 4F 4B... 为此,您可以使用例如 sprintf(aString, "%x", aChar)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文