Solved

file size question

Posted on 2006-06-30
5
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

738 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