Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem in reading a Hex file and then.....

Reply
Thread Tools

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

 
 
rsk
Guest
Posts: n/a
 
      07-20-2007
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...





 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      07-20-2007
rsk wrote:

> 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.

 
Reply With Quote
 
 
 
 
rsk
Guest
Posts: n/a
 
      07-20-2007
Thank you so much santosh.So kind of you...


With Best Regards,
Ravi

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-20-2007
"rsk" <(E-Mail Removed)> writes:
> 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?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      07-21-2007
Thad Smith wrote:
> santosh wrote:
>> rsk wrote:


[ ... ]

>> 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.


 
Reply With Quote
 
rsk
Guest
Posts: n/a
 
      07-21-2007

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
 
Reply With Quote
 
Thad Smith
Guest
Posts: n/a
 
      07-21-2007
santosh wrote:
> 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.

--
Thad
 
Reply With Quote
 
rsk
Guest
Posts: n/a
 
      07-21-2007
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?



 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-21-2007

"Thad Smith" <(E-Mail Removed)> wrote in message
news:46a1a10f$0$79255$(E-Mail Removed) anews.com...
> santosh wrote:
>> 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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
rsk
Guest
Posts: n/a
 
      07-21-2007
Hi friends,

the command.txt contains a sequence of hex numbers
like

adcdef45
12435fde
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
[Fwd: Re: hex string to hex value] tim Python 2 11-23-2005 07:18 AM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments