Solved

file size question

Posted on 2006-06-30
5
268 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pointer in one class to member in another 6 121
Detect CR LF to each line 12 161
Can case within switch statement specify range of values ? 3 81
outlook office 365 8 122
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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

810 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