Hex format reading from a file

Mar 01, 2007 6 Replies

Hi,



I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char. I am able to read the data from the file, which i am getting as characters.



For example: Suppose the Hexa value from the File data is ABCDEFGH. In hexa form 0xAB is one byte. So, while reading this data A is taken as a byte which is not a required format. I am supposed to get AB as a byte, which i am not getting.



Hope you people understand my query.



with regards,



Chandra.



Hope you understand:

a) this is the wrong newsgroup to post a Computer Science 101 question, and

b) without specifying anything about the file format, delimiters, or even the programming language you're using, you haven't in fact asked a question yet; you've merely stated that you have a problem.

0xGH? Must be the difference between hex and hexa.

Scott

This is a very common, basic embedded coding task. Assuming you do it by hand and not using some library function:

Read in two 8-bit chars. Convert each to a 4-bit hex nibble (digit) (you will need to handle the numbers and the letters seperately, or use a lookup table) Multiple the first by 0x10 and add it to the second

On systems where you can afford to waste 64kB like that, your int is probably 32 bits.

Or, if you can afford a 64K LUT you could do

int *si = (int *)str; char *hex = someOutputBuffer; while (*si) hex++ = bigLut[*si++];

assuming str is properly terminated with two nulls.

Andrew

Nice catch! Ok, version 2.0:

short *si = (short *)str; char *hex = someOutputBuffer; while (*si) hex++ = bigLut[*si++];

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required