BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Reading binary data into different data types

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Students
View previous topic :: View next topic  
Author Message
JRB
Guest





PostPosted: Mon Jan 22, 2007 7:45 am    Post subject: Reading binary data into different data types Reply with quote



Well, after many hours of trial and errror, I was finally able to
get the data I was after. It was only after I typecast the values
I was retrieving from the binary file as Cardinal (and played
around with the number of bytes) did it come out as expected.

My question is thus: Could someone please explain (or point me
to a good resource) why reading 8 bytes of data could cause it to
be different based on the value I'm reading it into? For example,
in the following, iValue is an integer and dValue is a double.
Why would the results be different?

var
binStream : TFileStream
begin
...initialize stream and open file...
binStream.Read(iValue, SizeOf(Integer));
...iValue now equals 12345, but if I were to execute the
following instead...
binStream.Read(dValue, SizeOf(Double));
...dValue would equal 1234567890 (these are examples and not
really the values that are read -- suffice it to say that
the values are different however)

The same number of bytes were read, why are the values
of the variables now holding different values?

Could anyone help me understand this?

Thanks -
jrb
Back to top
Rob Kennedy
Guest





PostPosted: Mon Jan 22, 2007 7:57 am    Post subject: Re: Reading binary data into different data types Reply with quote



JRB wrote:
Quote:
Well, after many hours of trial and errror, I was finally able to
get the data I was after. It was only after I typecast the values
I was retrieving from the binary file as Cardinal (and played
around with the number of bytes) did it come out as expected.

My question is thus: Could someone please explain (or point me
to a good resource) why reading 8 bytes of data could cause it to
be different based on the value I'm reading it into? For example,
in the following, iValue is an integer and dValue is a double.
Why would the results be different?

For one thing, Integer is only four bytes. The other four bytes are
going somewhere else and are likely corrupting the rest of your program.

Second, Double is a floating-point type. It stores its data differently.
That's why we have different data types. That's how the program knows
how to interpret the contents of memory. The memory just stores a
pattern of bits. The type associated with the data tells the program how
to interpret those bits. The Integer type uses two's complement to store
its information. The Double type uses the IEEE 754 floating-point format.

Quote:
var
binStream : TFileStream
begin
...initialize stream and open file...
binStream.Read(iValue, SizeOf(Integer));
...iValue now equals 12345, but if I were to execute the
following instead...
binStream.Read(dValue, SizeOf(Double));
...dValue would equal 1234567890 (these are examples and not
really the values that are read -- suffice it to say that
the values are different however)

The bit pattern for 12345 as an Integer is this:

0000 0000 0000 0000 0011 0000 0011 1001

That's 32 bits. If you go find the IEEE format for single-precision
numbers, you can find out what that pattern of bits represents as a
Delphi Single type. (For the Double type, you'll have to come up with 32
more bits.)

Your file just has a bunch of bits. Your program doesn't inherently know
what any of those bits mean. When you choose a data type for your
variables, you're assigning meaning to the bits. Choose a type that's
appropriate for the data you stored.

--
Rob
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Students All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.