473,399 Members | 4,254 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Utility to read a hexadecimal number as string

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

Nov 15 '05 #1
3 2439
In article <11**********************@g47g2000cwa.googlegroups .com>,
Shivani <sh**************@yahoo.com> wrote:
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.
--
Look out, there are llamas!
Nov 15 '05 #2
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."
Nov 15 '05 #3
Emmanuel Delahaye wrote:
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.


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.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: thomasp | last post by:
I have not had any luck with this on other groups so I am posting it here. I created a .rtf file using MS Word that included text and graphics(.png file) . I then viewed this file with notepad. ...
10
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal...
1
by: Fernando Barsoba | last post by:
Hi all, First of all, I'd like to thank you "Skarmander" and "Flash Gordon" for the help they provided me: Skarmander's algorithm and Flash's modifications helped me a lot. Here's the problem...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
14
by: dharmdeep | last post by:
Hi friends, I need a sample code in C which will convert a Hexadecimal number into decimal number. I had written a code for that but it was too long, I need a small code, so request u all to...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
0
by: Onecarb | last post by:
Hi, i need help for a (maybe) simple function :). I need to send by serial port a series of hexadecimal bytes with a xor control character. The language i use could not work with hexadecimal and...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.