Skip to main content
GameDev.net gamedev.net
🔒 Locked

Binary input into a short?

Started by RenegadeKoolAid Mar 1, 2003 at 2:03 AM 5 replies 250+ views
Original Post
RenegadeKoolAid
RenegadeKoolAid
Hey everyone, Im working on a lossy compression program for school over spring break here (great homework huh), and its quadtree compression of a picture in binary format. But the professor made up his own kind of picture format, with a .[1] extension and the first 4 bytes of the file are the dimensions of the picture followed by the rest of the picture data, where each byte represents a pixel. My dumb question is, how do I get those first 4 bytes (2 for rows, 2 for columns, which is why Im using shorts) into short variables? This is a really dumb question I know, I probably have some lame syntax bug somewhere but heres what I have:

short imgSizeRows;
short imgSizeCols;

fin.open( argv[1], ios::binary ); 
fin.read( (char*)&imgSizeRows, sizeof(imgSizeRows) );  
fin.read( (char*)&imgSizeCols, sizeof(imgSizeCols) ); 

 
Where imgSizeRows & imgSizeCols are private variables in the quadtree class. Right after I read in the dimensions I allocate a 2D array for the rest of the pixels and fill in that array, but for some reason the code above I always get 2''s for the two variables Any suggestions?
RenegadeKoolAid
RenegadeKoolAid
Ok I opened the file with a hex editor and the first four bytes are: 02 00 02 00, which should be 512 x 512, for the image dimensions. Anyone know why when I read those first 4 bytes in using the above method i get an output of 2's for the short variables?


[edited by - RenegadeKoolAid on March 1, 2003 3:26:34 PM]
Null and Void
Null and Void
The file is big endian, your platform is little endian.

RenegadeKoolAid
RenegadeKoolAid
How would I change that? I thought this would be big endian by default.
Xanth
Xanth
You could also, for WORDs (shorts), rotate 8 bits to left or right...


asm ror imgSizeRows, 8;
"I thought Genius lived in bottles..." - Patrick Star

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.