473,396 Members | 1,766 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,396 software developers and data experts.

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

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

Jul 20 '07 #1
13 3569
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.

Jul 20 '07 #2
rsk
Thank you so much santosh.So kind of you...
With Best Regards,
Ravi

Jul 20 '07 #3
"rsk" <kr******@yahoo.co.inwrites:
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) ks***@mib.org <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"
Jul 20 '07 #4
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
Jul 21 '07 #5
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.
Jul 21 '07 #6
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
Jul 21 '07 #7
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?

Jul 21 '07 #8

"Thad Smith" <Th*******@acm.orgwrote in message
news:46***********************@auth.newsreader.oct 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

Jul 21 '07 #9
rsk
Hi friends,

the command.txt contains a sequence of hex numbers
like

adcdef45
12435fde
Jul 21 '07 #10
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.
/* 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 */
--
pete
Jul 21 '07 #11
Malcolm McLean wrote:
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.

--
Thad
Jul 21 '07 #12
"rsk" <kr******@yahoo.co.inwrites:
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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"
Jul 22 '07 #13
rsk wrote:
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.

Jul 22 '07 #14

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

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
5
by: Scott M. Lyon | last post by:
I've just discovered a bug in some code I wrote a little while ago, and I need you guys' help to fix it. My program imports data from a standard Excel Spreadsheet (just with specific column...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.