将文件的 hexdump 或 RAW 数据提取为文本

发布于 2024-10-24 00:41:11 字数 156 浏览 6 评论 0原文

我想知道是否有办法将文件的 hexdump 或原始数据输出到 txt 文件。 例如 我有一个文件“data.jpg”(文件类型无关)如何将 HEXdump(14ed 5602 等)导出到文件“output.txt”? 另外我如何指定输出的格式,例如 Unicode 或 UTF? 在 C++ 中

I was wondering if there is a way to output the hexdump or raw data of a file to txt file.
for example
I have a file let's say "data.jpg" (the file type is irrelevant) how can I export the HEXdump (14ed 5602 etc) to a file "output.txt"?
also how I can I specify the format of the output for example, Unicode or UTF?
in C++

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

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

发布评论

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

评论(3

ゞ记忆︶ㄣ 2024-10-31 00:41:11

您可以使用循环、fread 和 fprintf:通过 read,您可以获取字节的字节值,然后通过 fprintf,您可以使用 %x 将十六进制打印到文件中。

http://www.cplusplus.com/reference/clibrary/cstdio/恐惧/

http://www.cplusplus.com/reference/clibrary/cstdio/ fprintf/

如果你希望它更快,你可以加载整个机器字(int 或 long long)而不是单个字节,如果你希望它更快,你可以读取整个数组,然后 sprintf 整个数组,然后 fprintf 该数组到文件中。

You can use a loop, fread and fprintf: With read you get the byte-value of the bytes, then with fprintf you can use the %x to print hexadecimal to a file.

http://www.cplusplus.com/reference/clibrary/cstdio/fread/

http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/

If you want this to be fast you load whole machine-words (int or long long) instead of single bytes, if you want this to be even faster you fread a whole array, then sprintf a whole array, then fprintf that array to the file.

过去的过去 2024-10-31 00:41:11

也许是这样的?

#include <sstream>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <algorithm>

int main()
{
    std::stringstream buffer( "testxzy" );
    std::istreambuf_iterator<char> it( buffer.rdbuf( ) );
    std::istreambuf_iterator<char> end; // eof

    std::cout << std::hex << std::showbase;

    std::copy(it, end, std::ostream_iterator<int>(std::cout));

    std::cout << std::endl;

    return 0;
}

您只需将 buffer 替换为读取二进制文件的 ifstream,然后使用 ofstream 而不是 将输出写入文本文件>cout

Maybe something like this?

#include <sstream>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <algorithm>

int main()
{
    std::stringstream buffer( "testxzy" );
    std::istreambuf_iterator<char> it( buffer.rdbuf( ) );
    std::istreambuf_iterator<char> end; // eof

    std::cout << std::hex << std::showbase;

    std::copy(it, end, std::ostream_iterator<int>(std::cout));

    std::cout << std::endl;

    return 0;
}

You just have to replace buffer with an ifstream that reads the binary file, and write the output to a textfile using an ofstream instead of cout.

狼性发作 2024-10-31 00:41:11

这是相当古老的——如果你想要 Unicode,你必须自己添加它。

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

int main(int argc, char **argv) {
    unsigned long offset = 0;
    FILE *input;
    int bytes, i, j;
    unsigned char buffer[16];
    char outbuffer[60];

    if ( argc < 2 ) {
        fprintf(stderr, "\nUsage: dump filename [filename...]");
        return EXIT_FAILURE;
    }

    for (j=1;j<argc; ++j) {

        if ( NULL ==(input=fopen(argv[j], "rb")))
            continue;

        printf("\n%s:\n", argv[j]);

        while (0 < (bytes=fread(buffer, 1, 16, input))) {
            sprintf(outbuffer, "%8.8lx: ", offset+=16);
            for (i=0;i<bytes;i++) {
                sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
                if (!isprint(buffer[i]))
                    buffer[i] = '.';
            }
            printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
        }
        fclose(input);
    }
    return 0;
}

This is pretty old -- if you want Unicode, you'll have to add that yourself.

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

int main(int argc, char **argv) {
    unsigned long offset = 0;
    FILE *input;
    int bytes, i, j;
    unsigned char buffer[16];
    char outbuffer[60];

    if ( argc < 2 ) {
        fprintf(stderr, "\nUsage: dump filename [filename...]");
        return EXIT_FAILURE;
    }

    for (j=1;j<argc; ++j) {

        if ( NULL ==(input=fopen(argv[j], "rb")))
            continue;

        printf("\n%s:\n", argv[j]);

        while (0 < (bytes=fread(buffer, 1, 16, input))) {
            sprintf(outbuffer, "%8.8lx: ", offset+=16);
            for (i=0;i<bytes;i++) {
                sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
                if (!isprint(buffer[i]))
                    buffer[i] = '.';
            }
            printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
        }
        fclose(input);
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文