Solved

file size question

Posted on 2006-06-30
5
262 Views
Last Modified: 2012-05-05
I'm attempting to learn how to read file formats and am having some trouble figuring out what is going on here. The format description states:

"The value for file length is the total length of the file in 16-bit words (including the fifty
16-bit words that make up the header)."

This is how the file length is stored in the header:
Byte 24    File Length    Integer    Big Endian

I read the value from byte 24 as 4 bytes (integer size) in Big endian and I get the value 10106 which I thought would be valid, however I found someone else that wrote a library on the internet (w/ source code) that works and I'm trying to understand what they are doing as they get the value 20212 when I run it. Their code is:

    psSHP->nFileSize = (pabyBuf[24] * 256 * 256 * 256
                  + pabyBuf[25] * 256 * 256
                  + pabyBuf[26] * 256
                  + pabyBuf[27]) * 2;

I don't understand why they are multiplying by 256 to figure up the file size? And why is the right-most value multiplied by 2? thanks for any help!
0
Comment
Question by:guidway
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 200 total points
ID: 17020045
2 is because value in 16-bit words is converted to bytes.
Other is binary arithmetic, like this decomal sample:
132 = 1 * 10^2 + 3 * 10 + 2
0
 
LVL 12

Author Comment

by:guidway
ID: 17020244
So if it is being converted to bytes, couldn't you just read all 4 bytes (as I did) as an integer for filesize and then multiply by 2 to get the number of bytes or is that not always going to give the same result?

I guess I'm trying to figure out why they are breaking each value down instead of reading all 4 bytes at once. (I'm actually writing this in C#, but I posted here since it was C code).

I thought I was making some progress understanding this, but its taking a while to sink in fully.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17020281
This is done because value is kept in Big Endian format, and code runs on Little Endian computer. Simple conversion of 4 bytes gives incorrect result.
0
 
LVL 16

Assisted Solution

by:PaulCaswell
PaulCaswell earned 200 total points
ID: 17020583
Hi guidway,

Notice that x * 256 == x << 8 so 'pabyBuf[24] * 256 * 256 * 256' is equivalent to 'pabyBuf[24] << (8+8+8)'.

Paul
0
 
LVL 12

Author Comment

by:guidway
ID: 17020698
that makes sense. I never realized shifting by 8 is the same as multiplying by 256. I see I still have a lot to learn. Thanks to both of you, that helps a lot. Increasing to 400 and splitting.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now