Luigi Auriemma

aluigi.org (ARCHIVE-ONLY FORUM!)
It is currently 19 Jul 2012 14:42

All times are UTC [ DST ]





Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: [C]Hex from file to ASCII
PostPosted: 30 Apr 2009 22:28 

Joined: 17 Mar 2009 21:04
Posts: 7
Need help again, working on a program that will take a string inputted by the user and write it to a file in hex form, I have done this but I also want it to be able to read hex from files and output it to ascii this is where I am having problems I began the function:

Code:
int readdata(void)
{
char file_name[40];
char filedata[9000];
int q=0;      /*number of characters file has*/
int count;     /*EOF flag from file*/
int i=0;
FILE *fp;
gets(file_name);
fp=fopen(file_name, "r");
if(fp==null) {
       printf("cannot find file");
       exit(8);
       }
while(1) {
ch=fgetc(fp);
count++;
if(count==EOF) break;
}
filedata=fscanf(fp, "%[^' ']%*x", struct.memberSTRING);
}


I am stumped (from this point) & am %99.9 certain that is entirely wrong can anyone help?


Top
 Profile  
 
 
 Post subject: Re: [C]Hex from file to ASCII
PostPosted: 30 Apr 2009 23:42 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
depends all by the format of the input for making the tool more or less compatible (for example my hex2byte tool is written with compatibility in mind), anyway the following is a simple example able to catch any sequence of hex numbers attached or separated by spaces:
Code:
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {
    FILE    *fd,
            *fdo;
    int     c;

    if(argc < 3) exit(1);

    fd  = fopen(argv[1], "rb");
    fdo = fopen(argv[2], "wb");
    if(!fd || !fdo) exit(1);

    while((fscanf(fd, "%02x", &c) == 1)) {
        fputc(c, fdo);
    }

    fclose(fd);
    fclose(fdo);
    return(0);
}


Top
 Profile  
 
 Post subject: Re: [C]Hex from file to ASCII
PostPosted: 01 May 2009 01:08 

Joined: 17 Mar 2009 21:04
Posts: 7
you sir, are a genius!


Top
 Profile  
 
 Post subject: Re: [C]Hex from file to ASCII
PostPosted: 01 May 2009 02:11 

Joined: 13 Aug 2007 21:44
Posts: 4068
Location: http://aluigi.org
all merit of the *scanf functions :)

just remaining in theme of *scanf remember to never use the short and char form for specifying the output numbers, for example "char c; sscanf(buffer, "%02hhx", &c);" or "char c[1000]; for(blah) sscanf(buffer, "%02hhx", c);" because there is the risk to corrupting some parts of the adjacent memory.
so even if you are an optimization maniac and the output is a buffer use ever a temporary integer for placing the captured value of scanf.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for: