Archived

This topic is now archived and is closed to further replies.

RenegadeKoolAid

Binary input into short variables?

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites