Problem in reading a Hex file and then.....

R

rsk

Hi Friends,

My requirement is as follows;

A file is consisting of data in hexadecimal format(i.e a 32 bit data for
example like "0xdeadbeef").

I have to read each of such data into my 'c' code
and i need to assign them to a 32 bit integer array.

Then i have to part select this array bit by bit like array[31]
array[30]...........array[0] and do some specific operations.

I am facing difficulty in assigning this 32 bit hex data to the 32 bit
array.

Can you please kindly help me on this.

With Best Regards,
RSK...
 
S

santosh

rsk said:
Hi Friends,

My requirement is as follows;

A file is consisting of data in hexadecimal format(i.e a 32 bit
data for example like "0xdeadbeef").

I have to read each of such data into my 'c' code
and i need to assign them to a 32 bit integer array.

Then i have to part select this array bit by bit like array[31]
array[30]...........array[0] and do some specific operations.

I am facing difficulty in assigning this 32 bit hex data to the
32 bit array.

Can you please kindly help me on this.

If you know the size of your file you can declare the exact size
of the array during compile-time. Otherwise you need to resize
the array on-the-fly as you program reads more of the file.

In any case, make the type of the array as long, and open the
file with fopen in binary read mode. Then use fread in a loop to
fill the array of longs. Stop when fread returns a less than
expected read count. You can distinguish between end-of-file and
a read error by testing with feof or ferror.

The do whatever you want with the array. If it's dynamically
allocated don't forget to free it when you're done. Similarly
close the file after reading it.

In C, the type long is guaranteed to be 32 bits. The type int may
or may not be. If you want a type exactly of 32 bits use
int32_t, though beware that it's improperly supported C99. I'd
just use long.

For any more help, post the code you've written to do what you
want and point out the exact place where you're having
difficulty. DO NOT RETYPE CODE. CUT & PASTE IT.
 
K

Keith Thompson

rsk said:
My requirement is as follows;

A file is consisting of data in hexadecimal format(i.e a 32 bit data for
example like "0xdeadbeef").
[snip]

Let's be sure we really understand your data format, since I've seen a
lot of confusion on this point.

Does the file actually contain data represented in hexadecimal? In
other words, for the value 3735928559 (0xdeadbeef), does the file
actually contain the (probably ASCII) characters 'd', 'e', 'a', 'd',
'b', 'e', 'e', 'f'? (And if so, are they preceded by the characters
'0', 'x'?) Or are 32-bit numbers stored in the file in a 32-bit
binary format?

We've seen people here refer to raw binary as "hexadecimal", probably
because that's how binary files are often *viewed*. But in fact,
binary ahd hexadecimal are very different formats.

If the values are stored in hex, how are the values separated? Does
each hexadecimal literal occur on a line by itself (terminated with
'\n')? Or are there multiple literals per line, and are they
separated by spaces, tabs, commas, arbitrary whitespace, or something
else?
 
S

santosh

[ ... ]
If you want a type exactly of 32 bits use int32_t, though beware that
it's improperly supported C99.

Why do you say that int32_t is improperly supported [in] C99?

I should have written that better. What I meant was C99 itself was
improperly supported by compiler vendors. Hence if the OP decides to use
C99 types, he might encounter compilation problems when moving his code to
an implementation that partially or fully lacks support for C99, like MS
Visual C++ and many compilers targetting embedded platforms.
 
R

rsk

No they don't precede by the charecters like '0', 'x',Actually the .txt
file contains the commands in hex format as
deadbeef
00000001
00000002
00000003
00000004
00000005
00000006
00000007
00000008
00000009
0000000a
0000000b
0000000c
0000000d
0000000e
0000000f
00000010
00000011
00000012
00000013
00000014
 
T

Thad Smith

santosh said:
rsk wrote:
In C, the type long is guaranteed to be 32 bits.

Variables of type long are guaranteed to have /at least/ 32 bits. They
may have more.
The type int may
or may not be. If you want a type exactly of 32 bits use
int32_t, though beware that it's improperly supported C99.

Why do you say that int32_t is improperly supported [in] C99?
For any more help, post the code you've written to do what you
want and point out the exact place where you're having
difficulty. DO NOT RETYPE CODE. CUT & PASTE IT.

Good advice.
 
R

rsk

Santosh,i have a doubt when the file is containing data in hex for mat can
i open the file with fopen in binary mode?
 
M

Malcolm McLean

Thad Smith said:
santosh said:
rsk wrote:
In C, the type long is guaranteed to be 32 bits.

Variables of type long are guaranteed to have /at least/ 32 bits. They
may have more.
The type int may
or may not be. If you want a type exactly of 32 bits use
int32_t, though beware that it's improperly supported C99.

Why do you say that int32_t is improperly supported [in] C99?
int32_t may exist or it may not. Your compiler may be an old compiler or it
may be a compiler with a few C99 features. In future your code might even
have to be run through a conforming C99 compiler.
This causes huge problems, and the only answer is to be extremely
conservative.
 
P

pete

rsk said:
Hi Friends,

My requirement is as follows;

A file is consisting of data in hexadecimal format(i.e a 32 bit data for
example like "0xdeadbeef").

I have to read each of such data into my 'c' code
and i need to assign them to a 32 bit integer array.

Then i have to part select this array bit by bit like array[31]
array[30]...........array[0] and do some specific operations.

I am facing difficulty in assigning this 32 bit hex data to the 32 bit
array.

Can you please kindly help me on this.

/* BEGIN new.c */

#include <stdio.h>

#define READ_UBIT(U, N) ((U) >> (N) & 1u)
#define BITS 32

int main(void)
{
int array[BITS];
long unsigned hex = 0xdeadbeef;
unsigned index;

for (index = 0; index != BITS; ++index) {
array[index] = READ_UBIT(hex, index);
}
puts("/* BEGIN new.c output */\n");
printf("hex is 0x%lx\n\n", hex);
for (index = 0; index != BITS; ++index) {
printf("array[%2u] is %d\n", index, array[index]);
}
puts("\n/* END new.c output */");
return 0;
}

/* END new.c */
 
T

Thad Smith

Malcolm said:
int32_t may exist or it may not. Your compiler may be an old compiler or
it may be a compiler with a few C99 features. In future your code might
even have to be run through a conforming C99 compiler.
This causes huge problems, and the only answer is to be extremely
conservative.

Even on a C99 conforming compiler int32_t isn't required to exist. For
portable code, I can use variables that are at least the required length
and not depend on the exact length.
 
K

Keith Thompson

rsk said:
No they don't precede by the charecters like '0', 'x',Actually the .txt
file contains the commands in hex format as
deadbeef
00000001
00000002 [snip]
00000013
00000014

*Please* provide some context when you post a followup. If you're
using "talkaboutprogramming.com" as an interface to this newsgroup,
and if it doesn't do this for you automatically, please find another
way to post; even Google Groups would be better. Don't assume that
your readers can easily see the article to which you're replying (but
trim any quoted material that isn't relevant to your response,
particularly signatures). See most of the posts in this newsgroup for
examples.

The above is a response my question about the actual format of your
input file. So if I understand you correctly, you've got a text file
where each line contains an 8-digit hexadecimal number. If that's
correct (and it's something you should have told us to begin with),
then reading it is fairly straightforward. I'm not going to write the
code for you, but you can use fgets() to read a line, then something
like strtoul to convert each line to a number. You should also think
about error handling (what to do if an input line is too long, or if
it doesn't contain a valid hex number).
 
S

santosh

rsk said:
Santosh,i have a doubt when the file is containing data in hex for mat can
i open the file with fopen in binary mode?

Usually binary mode is used for non-text files. If your file is in text
format, i.e., you can use EDIT.COM to read it, then the method Keith
Thompson suggested is more straightforward. Read in each line using fgets
and convert the line into a numeric value using strtoul or similar. If your
data is strictly regular you may also use fscanf to read and convert is one
stroke, but that's a slippery slope.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top