<div>I am using module struct.unpack() to decode data from a binary file, so that I can use the value in a calculation.</div>
<div> </div>
<div>I have been able to extract an integer value.</div>
<div> </div>
<div> >>>length = struct.unpack('i', '\x9c\x00\x00\x00')<br> >>>length = int(length[0])<br> >>>print length</div>
<div> 156</div>
<div> </div>
<div>I want to be able to extract a string.</div>
<div> </div>
<div>I have tried,</div>
<div> </div>
<div> >>>first = struct.unpack('s', '\x02\x00')<br> >>>first = str(first[0])<br> >>>print first</div>
<div> Traceback (most recent call last):<br>......<br> error: unpack requires a string argument of length 1<br></div>
<div>and,</div>
<div> </div>
<div> >>>first = struct.unpack('cccc', '\x02\x00')<br> >>>first = str(first[0])<br> >>>print first</div>
<div> Traceback (most recent call last):</div>
<div>......<br>
return o.unpack(s)<br> error: unpack requires a string argument of length 4</div>
<div> </div>
<div>My desired result would be the string '0200'. Actually, I would like to be able to invert the bytes to get '0002'.</div>