473,398 Members | 2,212 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,398 software developers and data experts.

Reading a Binary File

Hi All:

I encounter a programming problem recently. I need to read a binary
file. I need to translate the binary data into useful information. I
have the format at hand, like 1st byte = ID, next 4 byte (int) =
serial number etc.

The first problem is Big Endian/ Little Endian problem. I can decipher
if the format is big or little endian. But got confuse as to how to
decipher the data.
Eg. if I know I am on little endian, and I have a integer whose binary
representation is
20 03 00 00, then what is the equivalent decimal?

The next problem is there are also floating point data. How can I
infer the floating point data from a binary representaiton, like what
r the numbers before the decimal point and those after the decimal
point?

I hope I am not too confusing. Any help gladly appreciated. BTW, my
language is C/C++
Nov 13 '05 #1
3 9582
Tanuki wrote:

Hi All:

I encounter a programming problem recently. I need to read a binary
file. I need to translate the binary data into useful information. I
have the format at hand, like 1st byte = ID, next 4 byte (int) =
serial number etc.

The first problem is Big Endian/ Little Endian problem. I can decipher
if the format is big or little endian. But got confuse as to how to
decipher the data.
Eg. if I know I am on little endian, and I have a integer whose binary
representation is
20 03 00 00, then what is the equivalent decimal?
Assuming that by "binary" you actually mean "hexadecimal,"
this value is decimal 800. How did I get there?

0x20 + 0x03*0x100 + 0x00*0x10000 + 0x00*0x1000000
or equivalently
((0x00 * 0x100 + 0x00) * 0x100 + 0x03) * 0x100 + 0x20
or equivalently
0x20 + (0x03<<8) + (0x00<<16) + (0x00<<24)
or equivalently
(((((0x00 << 8) + 0x00) << 8) + 0x03) << 8) + 0x20
The next problem is there are also floating point data. How can I
infer the floating point data from a binary representaiton, like what
r the numbers before the decimal point and those after the decimal
point?


Without more knowledge of the representation, you're stuck.
In the integer case you already knew a good deal about what the
representation looked like: you knew it consisted of four eight-
bit bytes arranged in Little-Endian order. (Some questions still
remain, of course: for example, what do negative integers look
like?) But in the floating-point case, all you've told us is
that you know the numbers are floating-point -- but if you know
nothing about the representation, there's no way to decode it.

The best thing to do is consult the documentation for the
system that wrote the file, and see whether it tells you how
floating-point numbers are stored.

Failing that, you could try inspecting the data in the file
and seeing whether it "looks like" a well-known floating-point
format. The commonest such formats are surely the IEEE single-
and double-precision binary floating point; try interpreting
the bits according to those formats and see whether the values
you get "make sense" for the application at hand. If it doesn't
look like IEEE, you could also try the various VAX floating-
point formats, or the S/360 base-16 formats.

Of course, if the numbers were written out on the same system
that's reading them back again, they'll be in some native format
supported by that system. If you can figure out which one, you
needn't sweat the details: just read the bits into an object of
the proper type, and you're done.

--
Er*********@sun.com
Nov 13 '05 #2
Bjoern Willenberg <si***@wivada.de> writes:
Tanuki wrote:
The next problem is there are also floating point data. How can I
infer the floating point data from a binary representaiton, like what
r the numbers before the decimal point and those after the decimal
point?


fread seems to be what you are looking for.


Barring additional translation of what's read from the file, only
if data formats in the file have the same representation as on
the machine's memory.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
Nov 13 '05 #3
On Fri, 01 Aug 2003 11:14:58 -0400, Tanuki wrote:
Hi All:

I encounter a programming problem recently. I need to read a binary
file. I need to translate the binary data into useful information. I
have the format at hand, like 1st byte = ID, next 4 byte (int) = serial
number etc.
The encdec package provides the primatives necessary to pick apart as
well as encode arbitrary binary formats:

http://www.ioplex.com/~miallen/encdec/
The first problem is Big Endian/ Little Endian problem. I can decipher
if the format is big or little endian. But got confuse as to how to
decipher the data.
Eg. if I know I am on little endian, and I have a integer whose binary
representation is
20 03 00 00, then what is the equivalent decimal?
This is 0x00000320 in hex. Use calc.exe in scientific mode, kcalc, or similar
to see what it would be in decimal. To decode this properly you should
use a function like this:

uint32_t
dec_uint32le(const unsigned char *src)
{
return src[0] | ((unsigned)src[1] << 8) |
((unsigned)src[2] << 16) | ((unsigned)src[3] << 24);
}

If the data was big-endian you would use the dec_uint32be function. You
can also use the enc_ functions to encode the format. Etc...
The next problem is there are also floating point data. How can I infer
the floating point data from a binary representaiton, like what r the
numbers before the decimal point and those after the decimal point?
If it's a reasonable format and not something that the last guy just
made up, chances are floating point values are encoded using the standard
IEEE754 encoding. See {enc,dec}_{float,double}{le,be} functions again
from the encdec package.
I hope I am not too confusing. Any help gladly appreciated. BTW, my
language is C/C++


This actually comes up quite frequently believe it or not.

Keep in mind that if you know the format is just the contents of a struct
in memory writen to disk using fwrite you should probably just read it
out that way too. That will be faster. On the other hand, be aware you
cannot just fwrite a structure to a disk file, transfer it to another
computer, and fread it out again. I think you understand there is a
potential for endianess and structure member padding issues.

Mike
Nov 13 '05 #4

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

Similar topics

4
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
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
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
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
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...
30
by: siliconwafer | last post by:
Hi All, I want to know tht how can one Stop reading a file in C (e.g a Hex file)with no 'EOF'?
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;
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
3
by: The Cool Giraffe | last post by:
Regarding the following code i have a problem. void read () { fstream file; ios::open_mode opMode = ios::in; file.open ("some.txt", opMode); char *ch = new char; vector <charv; while...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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
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...
0
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...

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.