473,668 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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]...........arra y[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 3591
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]...........arra y[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 "hexadecima l", 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_Keit h) 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.newsreade r.octanews.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

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

Similar topics

1
7044
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 file... Thank you to all of you who can help me with this one...
8
9846
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 | ios::binary) to declare a file object with read/write modes turned on for working with binary data. I've tried this and my file is not created. The only time it is created is when I specify ifstream or ofstream but not fstream. I've tried removing the...
7
5430
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
2477
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() { OperatingSystem os = Environment.OSVersion; AppDomain ad = Thread.GetDomain();
7
2913
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 database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
5
2725
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
8936
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 headers). I used ODBC in my VB.NET program to read that spreadsheet into a dataset, to make it easy to manipulate. The code I use to read it is as the bottom of this posting.
6
5262
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
14981
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 and why I have to do it. Hopefully someone has a suggestion... Alright, so I'm using a gps-simulation program that outputs gps data, like longitude, lattitude, altitude, etc. (hundreds of terms, these are just the well known ones). In the newer...
6
3524
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 to an old program that I wrote in delphi and it's a good opportunity to start with c++.
0
8462
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8893
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8802
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8586
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8658
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4384
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2792
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2028
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.