You should look into the struct module. For example, you could do the same thing via (using the variable names you used before):<div><br class="webkit-block-placeholder"></div><div>header_str = info.read(13)</div><div>a,b,c,d,e = struct.unpack("6s4sBBB", header_str)</div>
<div><br class="webkit-block-placeholder"></div><div>After that, you will probably be able to get the integers by (doing it one at a time... read'ing more than 2 bytes at a time and processing it all at once is going to be more efficient, but this is the basic idea):</div>
<div><br class="webkit-block-placeholder"></div><div>read_str = info.read(2)</div><div>val = struct.unpack("H", read_str)</div><div><br class="webkit-block-placeholder"></div><div>Read up on the struct module and I think you'll see what you need to do. (note that if you arent getting the right numbers for val, you will need to specify whether the data is big-endian or little-endian.... if you dont know what that means, you will find it on Wikipedia)</div>
<div><br class="webkit-block-placeholder"></div><div>Jared<br><div><br class="webkit-block-placeholder"></div><div><span class="Apple-style-span" style="font-family: Helvetica; font-size: 12px; "><div><br class="Apple-interchange-newline">
On 4 Feb 2008, at 19:51, Mastastealth wrote:</div><blockquote type="cite">I'm trying to create a program to read a certain binary format. I have<br>the format's spec which goes something like:<br><br>First 6 bytes: String<br>
Next 4 bytes: 3 digit number and a blank byte<br>---<br>Next byte: Height (Number up to 255)<br>Next byte: Width (Number up to 255)<br>Next byte: Number 0 - 5<br>Every 2 bytes after that: Supposedly a number 0000 - 0899?<br>
<br>Anyway, I'm able to do the first 2 objects fine:<br><br>a = info.read(6)<br>b = info.read(4)<br><br>Printing both gives me what I mentioned above, a string and a 3 digit<br>number with a space. However, as I continue, things get trickier.<br>
<br>c = info.read(1)<br>d = info.read(1)<br><br>Printing c and d in this case gives me a block in the SPE output, or<br>if I run in a DOS prompt, 2 funny symbols. How do I get an integer out<br>of this? I'll probably need help once I get to the "every 2 byte"<br>
section, but that'll be a separate post.<br>-- <br><a href="http://mail.python.org/mailman/listinfo/python-list">http://mail.python.org/mailman/listinfo/python-list</a><br></blockquote><div><br class="webkit-block-placeholder">
</div></span></div></div>