Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hex2bin 1

Status
Not open for further replies.

riteshshah

Programmer
Feb 8, 2005
12
US
Folks,

Anyone have any idea on where I can find some 'c' code (on sun solaris 2.8) that will convert a hex file to binary?

Thanks!
 
All files are binary. Could you better describe what you are trying to do?
 
Hi Dave,

Yep, basically I have an application that converts a tif file (in effect the format of any file received over FAX) into hex so it can be easily routed via MQ or SMTP, and I need to build an app. to convert it back to a tif. I've got some code I've built in C to do the mq get and record the message. I can also parse for the section relating to the hex and then write this into a temp. file. Then I need to call a routine that'll convert a hex file into binary and then I can further process the final message and send to our archival app.

I've got an executable that will convert in dos (windows app) but I want some a 'c' code equiv.

Thanks,
 
Let me see if I understand this correctly:

You have a file for input, the data in which is text that contains the hexadecimal value representation of bytes. For example,
Code:
48656C6C6F20776F726C64
You want to take this input file and convert this text and convert it into the byte it represents. You would then get an output like this,
Code:
Hello world
If that is so, you could start with something like this.
Code:
#include <stdio.h>

int main(void)
{
   static const char infilename[]  = "file.txt";
   static const char outfilename[] = "file.bin";
   FILE *infile  = fopen(infilename, "r");
   FILE *outfile = fopen(outfilename, "wb");
   if ( infile && outfile )
   {
      int ch;
      while ( fscanf(infile, "%2x", &ch) == 1 )
      {
         fputc(ch, outfile);
      }
      fclose(infile);
      fclose(outfile);
   }
   return 0;
}
 
Hi Dave,

My final output should be a tif image. Someone has mentioned 'base64' in relation to this.. So, to summarise, a message comes into the application in a tif format. It is then converted from tif into a hex representation. I need to get this hex representation and convert it back to a tif image.

I'll give the code above a go.. and see what I get out,

Thanks for the help.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top