将十六进制文件转换为ASCII文件的代码 [英] Code for Converting Hexadecimal File to ASCII File

查看:171
本文介绍了将十六进制文件转换为ASCII文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的技术人员,

我只是想知道如何将十六进制文件转换为C语言中的ASCII文件?
它基本上是一个十六进制转储文件(由十六进制内容组成),我想对其进行扫描并将其转换为ASCII码.而且该Hex文件无法在记事本或写字板中打开,它与项目有关,并且只能在Visual Stduio IDE中查看.
希望大家对目标有所了解.
有人可以给我一些线索吗?.

提前谢谢.

问候,
Aditya.

解决方案

就目前而言,您的问题毫无意义.您的意思是如何写二进制文件的十六进制表示形式?".

如果答案是,那么您可能会发现以下代码很有用:

 #include   ><   stdio.h  > 
  int  main( int  argc, char  * argv [])
{
   int  n,计数;
  文件* fpi,* fpo;
  如果(argc!=  3 )
  {
    fprintf(stderr," ,argv [  0 ]);
    返回- 1 ;
  }
  fpi = fopen(argv [ 1 ]," );
  如果(!fpi)
  {
    printf(" ,argv [返回- 2 ;
  }
  fpo = fopen(argv [ 2 ]," );
  如果(!fpo)
  {
    printf(" ,argv [返回- 3 ;
  }
  count =  0 ;
   while ((n = fgetc(fpi))!= EOF)
  {
     int  i;
     for (i =  0 ; i< 2; i ++)
    {
      无符号 字符 b =(n& 0xF0)>>  4 ;
      如果(b<  10 )
        fputc(b + '  0',fpo);
      其他
        fputc(b-  10  + '  A',fpo);
      n<< =  4 ;
    }
    数++;
    // 可选... 
    如果(!(计数% 16 ))
      fputc('  \ n',fpo);
    其他
      fputc(' ',fpo);
  }
  fclose(fpo);
  fclose(fpi);
  返回  0 ;
} 



:)


mightytechie写道:

有人可以向我发送该代码吗? ..



实际上不是.真的,我们一点也不在乎.您应该计划好工作,以免受到陌生人的怜悯.


mightytechie写道:

而且十六进制文件无法在记事本或写字板中打开



那么,文件中的值不是文本,它们是不同的吗?没有十六进制文件之类的东西.我看不到任何转换,除非不知道原始数据的格式以及您希望将其转换为的格式.如果您可以在IDE中查看它,那么我认为这意味着这些值是ASCII值并且需要转换为字符,您可以通过将它们读取为字节,然后一次将其转换为char来实现.

哇,我给出了正确的答案,并说出了请求性质的真相,我得到了4 1票?如果我的回答没有帮助,则是由于以下两个原因之一.

1-您无法执行此基本任务.那不是我的错
2-您的数据在某种程度上与您的问题似乎编码方式有所不同.如果您想要更好的答案,请提供更多信息.我敢肯定,如果您提供了实际的数据以及希望从中获得的文本,那么我可以为您提供执行此操作的代码.但是,看到您1投票赞成我,我就不会投票,您可以下地狱.但是,我敢肯定,如果您能抽出适当的时间问清楚这个问题,而不是模糊的胡言乱语,那么其他人也会这么做.


Dear techies,

I just wanted to know how can we convert a Hexadecimal File to ASCII File in C?
Its is basically a Hex dump file (consists of hexadecimal content) and I wanted to scan it and convert it into an ASCII Code. And that Hex file cannot to opened in a notepad or wordpad, its related to a project and can be viewed only in Visual Stduio IDE.
I hope you guys got some idea regarding the objective.
Can anybody give me some clue for that..?

Thanks in advance.

Regards,
Aditya.

解决方案

As it stands, your question makes no sense. Do you mean "How can I write the hexadecimal representation of a binary file?".

If the answer is yes, then you may find the following code useful:

#include <stdio.h>
int  main(int argc, char * argv[])
{
  int n, count;
  FILE * fpi, *fpo;
  if (argc != 3)
  {
    fprintf(stderr, "usage %s <binary_filename> <ascii_filenam>\n", argv[0]);
    return -1;
  }
  fpi = fopen(argv[1], "rb");
  if (!fpi)
  {
    printf("unable to open file %s\n", argv[1]); 
    return -2;
  }
  fpo = fopen(argv[2], "w");
  if (!fpo)
  {
    printf("unable to open file %s\n", argv[2]); 
    fclose(fpi);
    return -3;
  }
  count=0;
  while ( (n=fgetc(fpi)) != EOF)
  {
    int i;
    for (i=0; i<2; i++)
    {    
      unsigned char b = (n & 0xF0) >> 4;
      if ( b < 10 )
        fputc(b + '0', fpo);
      else 
        fputc(b-10 + 'A', fpo);
      n <<= 4;
    }
    count++;
    // optional...
    if (!(count % 16)) 
      fputc('\n', fpo); 
    else
      fputc(' ', fpo);
  }
  fclose(fpo);
  fclose(fpi);
  return 0;
}



:)


mightytechie wrote:

Can anybody send me the code for that ..its very very urgent..



It''s actually not. We don''t care at all, really. You should plan your work so you don''t find yourself at the mercy of strangers.


mightytechie wrote:

And that Hex file cannot to opened in a notepad or wordpad



So, the values in the file, are not text, they are something different ? There''s no such thing as a hexadecimal file. There''s no conversion that I can see, not without knowing the format of the original data, and the format you want it to be converted to. If you can view it in the IDE, I assume that means that the values are ASCII values and need to be converted to characters, you can do this by reading them as bytes, then casting them to char, one at a time.


Wow, I gave the correct answer, as well as telling the truth about the nature of the request, and I got 4 1 votes ? If my answer does not help, it''s for one of two reasons.

1 - you''re incapable of this basic task. That''s not my fault
2 - your data is somehow encoded differently to what seems likely from your question. If you want a better answer, provide more information. I''m sure that if you provided the actual data, as well as the text you hope to get from it, I could give you code to do it. But, seeing as you 1 voted me, I''m not going to, you can go to hell. But, I''m sure someone else will do it, if you take the time to ask properly instead of this vague drivel.


这篇关于将十六进制文件转换为ASCII文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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