【问题标题】:Creating copy of binary file from hex representation of it从它的十六进制表示创建二进制文件的副本
【发布时间】:2012-02-20 09:01:52
【问题描述】:

我想复制我的二进制文件,但我需要从我的二进制文件的十六进制表示中复制它。

在第一个程序中,我使用二进制文件的十六进制表示创建 txt 文件:

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


}

在第二个中,我从 txt 文件中读取数据,然后将其写入二进制文件:

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

很遗憾,我无法打开我的副本。是什么原因?

【问题讨论】:

  • 从来没有读过大小 1,这既慢又愚蠢。你有一个缓冲区,填充它并遍历它的内容。 read = fread(buffer, 1, BYTE, file1);count += read;i = 0;while (i &lt; read){ sprintf(hex, "%02x", (unsigned int) buffer[i++]); fwrite(hex, 1, BYTE, file2) }
  • 另外,这如何编译char buffer[size]; ??这是 C ANSI 禁止的,这是什么类型的 C?你为什么不使用 malloc ?
  • 很抱歉我的C语言不高。我在编译方面没有问题。

标签: c binary hex


【解决方案1】:

您查看过文件内容吗?您将无法将十六进制表示形式冲刺到变量 hex,因为它的大小为 1 个字节。

变量 hex 声明为 hex[BYTE],其中 BYTE = 1,但您的 sprintf 格式字符串如下所示:"%02x" 即 2 个字节,那么您需要空间用于终止零。

写入文件时也是如此,您只需从十六进制字符串中写入 1 个字节。

将变量声明为:var[1] 毫无意义,您可以使用 var 顺便说一句。

除此之外,如果您无法成功打开文件,您还应该添加适当的错误处理。这意味着在调用 fopen 后检查文件指针,然后采取适当的措施。 perror() 将打印一个与 errno 对应的错误字符串,如果文件不存在,它将打印类似:“没有这样的文件或目录”或类似内容。

【讨论】:

  • 你能详细解释一下吗?
  • 更改为“%01x”没有帮助
  • 您不应该更改格式字符串!您应该更改缓冲区的大小,即 hex 它必须至少为 3 个字节。 bufferhex 都使用 BYTE,所以 hex 不能使用 BYTE。
  • 所以,假设我想从我的二进制文件中读取 4 个字符。为此,我创建了一个表缓冲区 [4] 并使用 fread(buffer, 1, 4, file1)。然后我使用 sprintf(hex, "%02x", (unsigned int) buffer[i++]),其中 hex 是一个包含 9 个元素的表。我理解对了吗?
  • 您唯一需要记住的是实际字节的差异,显然是 1 字节大小。还有一个字符串表示,其中每个字符是 1 个字节 + 一个终止 \0
【解决方案2】:

当你说你不能打开你的副本时,你的意思是你在fopen("test.txt", "r")中有错误?你检查了errno 的值吗?检查perror()strerror()。 此外,您在第二个程序中没有循环。

【讨论】:

  • 不,我的意思是我无法在我的 pdf 查看器中打开 test.pdf。
  • 您只将“text.txt”文件中的 2 个字节读入缓冲区并在“test.pdf”中写入 size 字节。仔细阅读 foo 答案并使用循环从 test.txt 读取字节并写入 test.pdf 直到“test.txt”结束:请参阅man fscanf 并研究它。
猜你喜欢
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 2014-10-30
  • 1970-01-01
  • 2023-04-03
相关资源
最近更新 更多