在文件转换HEX数据ASCII [英] Converting HEX data in a file to ascii

查看:184
本文介绍了在文件转换HEX数据ASCII的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要脱code的ASN.1标准二进制文件。我已经转换二进制文件为十六进制,并将其存储在一个文件中。现在我想这个十六进制转换为ASCII。现在遇到的问题是IM如何读取hex文件。

i want to decode an ASN.1 standard binary file. i have converted the binary file to hex and stored it in a file. now i want to convert this hex to ascii. The problem im having now is how to read the hex file.

该文件看起来这种方式,
81 01 32 82 0D 35 31 34 32 34 31 38 38 38,其中81是一个头,01是尺寸和32的数据。再次是82头,这继续下去。 如何从这个文件中读取和各个领域的区分present

the file looks this way, 81 01 32 82 0D 35 31 34 32 34 31 38 38 38 where 81 is a header, 01 is the size and 32 is the data. again 82 is the header and this goes on. how do i read from this file and differentiate between the various fields present.

我找遍了互联网这一点,但不可能得到满意的答复。所以有人可以帮助我前进的方向。我不希望任何code,只是想我怎么能做到这一点的过程。

i searched all over the internet for this, but couldnt get a satisfactory answer. so can someone help me with the way forward . i dont want any code, just want the procedure how i can do it.

推荐答案

我会先看标题,然后在一个循环中的数据。你可以用X-specifier读十六进制数(说你的文件被命名为 hexfile.txt

I would first read the header and then in a loop the data. You can read hexadecimal numbers with the "x"-specifier (say your file is named hexfile.txt):

#include <stdio.h>

int main ()
{

    FILE *stream;
    unsigned int h, l, d;

    if( (stream  = fopen( "hexfile.txt", "r" )) == NULL ) return 1;

    while (EOF != fscanf (stream, " %x %x", &h, &l))
    {
        printf ("%02X %02X\n",h,l);

        for (unsigned i=0; i<l; ++i)
        {
            if (EOF == fscanf (stream, " %x", &d)) break;
            printf ("%02X ",d);
        }
        puts ("");
    }

    fclose (stream);

    return 0;
}

这篇关于在文件转换HEX数据ASCII的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆