473,513 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading from a 64 bit data Binary File

4 New Member
HI I'm new to this forum

I'm having a peculiar problem
I'm trying to read a 64 bit data binary file

I have 5 integer variables a b c d e

the problem is I have to read the 64 bit data

like 0-7 bits as integer value to a
8-15 bits as int into b
16-18 bits as int into c
etc like that
for eg if 0-7 bits are 0011010 then a should be a int value of 26

my format of binary file looks like the following

00000000 070E0000160A04C0 031000000F010280 .......À.......€
00000010 0A090000131401C0 1E1700001A0601C0 .......À.......À
00000020 1A0B05001E1701C0 18110100031603C0 .......À.......À
00000030 090C0000130001C0 00120700140E03C0 .......À.......À
00000040 18150500010E0380 0B160600160602C0 .......€.......À
00000050 0A0E0000130B01C0 19010100050803C0 .......À.......À
00000060 1B0801000C0F03C0 011607001D1303C0 .......À.......À

so in the first line I'm interested only in 070E0000160A04C0 since this is the 64 bit data

so for the binary file I set the hex line length as 8 and the view changes to
lsb msb
00000000 070E0000160A04C0 .......À
00000008 031000000F010280 .......€
00000010 0A090000131401C0 .......À
00000018 1E1700001A0601C0 .......À
00000020 1A0B05001E1701C0 .......À
00000028 18110100031603C0 .......À
00000030 090C0000130001C0 .......À

Which format you think will be better

I'm completely new to reading binary files or binary format itself

Can you please let me know how to read specific bits to a specific variable

A code sample would be of great help
Thank you
Dec 12 '08 #1
7 4227
Banfa
9,065 Recognized Expert Moderator Expert
You can read specific bits out of a file, the minimum you can read is a byte. You need to read all the bytes of data you are interested in (8 if its 64 bits) and then use the bitwise operators to extract from those 8 bytes the bits you are interested in into your 5 integer variables.
Dec 12 '08 #2
vikuba
4 New Member
thanx I think it got what you are saying

but would like to know how to read the binary file byte wise

a small sample code would be of great help
thanx
Dec 12 '08 #3
Banfa
9,065 Recognized Expert Moderator Expert
C or C++?
Dec 12 '08 #4
vikuba
4 New Member
if only i knew a way to do it in C

I'm asking for your help regarding the code to do it

I do not know a way of reading a binary file byte wise using C

Please help me regarding the code
Dec 12 '08 #5
Banfa
9,065 Recognized Expert Moderator Expert
Look up the functions

fopen - opens the file
fread - reads data from the file
fclose - closes the file

in your C text book or in the links given in the sticky post at the top of this forum.
Dec 12 '08 #6
vikuba
4 New Member
@Banfa
Why do you think I would post it in the c/c++ forum if I did not know that it can be done using C or C++

o my god
thanx for the great help mr.Banfa

and realize that people post it in a forum after trying it first

requesting a solution in a forum would be the last option for anyone

thats it i'm outta here
Dec 12 '08 #7
Tassos Souris
152 New Member
One thing that you should consider is that when using binary files you definitely hurt the portability of your program. That is because data read in from a binary stream shall compare equal to data that were written earlier to that stream under the SAME implementation. Otherwise you do not know what you get. Well, even in the same implementation you might get some tricky null padding there (correct me if i am wrong)...
If you do that because you want to exercise binary formats then ok.
If you do that for a program that needs to write in binary files that you do not care about moving them to another machine then you should consider the easiest way of a struct definition and a fwrite call (then retrieving them with fread).
If you do that for a program that needs data to be shared within programs then learn XML :-)

But because i suspect you're doing that to learn then these function mentioned by Banfa are all you need :-) :
Dec 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6397
by: john smith | last post by:
Hi, I have a file format that is going to contain some parts in ascii, and some parts with raw binary data. Should I open this file with ios::bin or no? For example: filename: a.bin number of...
20
3016
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code:...
6
3753
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
50
4881
by: Michael Mair | last post by:
Cheerio, I would appreciate opinions on the following: Given the task to read a _complete_ text file into a string: What is the "best" way to do it? Handling the buffer is not the problem...
7
6034
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
6
5245
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;
11
3557
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
13
3660
by: swetha | last post by:
HI Every1, I have a problem in reading a binary file. Actually i want a C program which reads in the data from a file which is in binary format and i want to update values in it. The file...
6
3512
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...
6
4212
by: jcasique.torres | last post by:
Hi everyboy. I trying to create a C promang in an AIX System to read JPG files but when it read just the first 4 bytes when it found a DLE character (^P) doesn't read anymore. I using fread...
0
7267
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,...
0
7175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7391
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,...
0
7553
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
7542
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...
1
5100
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3235
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1609
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 ...
0
466
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.