Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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.
Posted
Updated 22-Feb-10 21:13pm
v2

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:

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


:)
 
Share this answer
 
v3
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.
 
Share this answer
 
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900