Utility to read a hexadecimal number as string

S

Shivani

Hi,
I am looking for a utility that reads hexadecimal numbers from a file
into a string. The numbers in the file are seperated by "," as
delimeter. Is there a standard C utility that can do that?

I am trying to read the hexadecimal numbers and trying to write this to
a buffer in binary format. To do so, I am reading the numbers in a
string format and then converting the string (ASCII) to hex and write
it in binary format in the buffer.

Any suggestions for utilities or any other way to do so?

Shiv
 
W

Walter Roberson

I am looking for a utility that reads hexadecimal numbers from a file
into a string. The numbers in the file are seperated by "," as
delimeter. Is there a standard C utility that can do that?

No, but you can synthesize it easily enough using %x scan formats
or strtoul() with a base of 16.
I am trying to read the hexadecimal numbers and trying to write this to
a buffer in binary format. To do so, I am reading the numbers in a
string format and then converting the string (ASCII) to hex and write
it in binary format in the buffer.
Any suggestions for utilities or any other way to do so?

Myself, I use a very simple state machine. The complexity
is in handling the possibility of single hex digits, and in handling
end of line. I find that 3 states suffices; you could probably get that
down to two states if end-of-line was just "whitespace" for your
purposes.
 
E

Emmanuel Delahaye

Shivani wrote on 01/09/05 :
I am looking for a utility that reads hexadecimal numbers from a file
into a string. The numbers in the file are seperated by "," as
delimeter. Is there a standard C utility that can do that?

If you want to read a line of text, use fgets(). Then you can get the
data from the line with sscanf() of strtol().

Time to open your C-book.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."
 
J

Joe Wright

Emmanuel said:
Shivani wrote on 01/09/05 :



If you want to read a line of text, use fgets(). Then you can get the
data from the line with sscanf() of strtol().

Time to open your C-book.

Opening the C-book is always a good suggestion. And keep it open. As I
look around here at home, I see K&R1, K&R2 and H&S5 in view.

The reading of a data file is always treacherous in C. You must know
precisely the format of the file, binary or text, etc.

If you don't know it exactly you are doomed from the outset.

Usage of *scanf() presumes a 'text' file that you know the exact format of.

Usage of fgets() presumes a 'text' file of 'lines'. It allows you to
test the line in any way you like before you assign its contents
somewhere. I like fgets().

If you play with pure 'binary' files, database tables, indexes, etc.,
you are really on your own. You must know the format exactly and be
prepared to write your own functions for extacting strings or whatever.
The Standard C Library is of little use with .PDF or other such files.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top