<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>&nbsp;</div>
<div>I have been able to extract an integer value.</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt;&gt;length = struct.unpack(&#39;i&#39;, &#39;\x9c\x00\x00\x00&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt;&gt;length = int(length[0])<br>&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt;&gt;print length</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp; 156</div>
<div>&nbsp;</div>
<div>I want to be able to extract a string.</div>
<div>&nbsp;</div>
<div>I have tried,</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;first = struct.unpack(&#39;s&#39;, &#39;\x02\x00&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;first = str(first[0])<br>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;print first</div>
<div>&nbsp;&nbsp;&nbsp; Traceback (most recent call last):<br>......<br>&nbsp;&nbsp;&nbsp; error: unpack requires a string argument of length 1<br></div>
<div>and,</div>
<div>&nbsp;</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;first = struct.unpack(&#39;cccc&#39;, &#39;\x02\x00&#39;)<br>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;first = str(first[0])<br>&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt;print first</div>
<div>&nbsp;&nbsp;&nbsp;&nbsp; Traceback (most recent call last):</div>
<div>......<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return o.unpack(s)<br>&nbsp;&nbsp;&nbsp;&nbsp; error: unpack requires a string argument of length 4</div>
<div>&nbsp;</div>
<div>My desired result would be the string&nbsp; &#39;0200&#39;.&nbsp; Actually, I would like to be able to invert the bytes to get &#39;0002&#39;.</div>