Decoding Byte/Bits to Binary
<p>I am using a Prologix GPIB-USB adaptor in a LISTEN only mode to decipher communication between two equipments (Semiconductor related namely Tester and Prober).</p>

<p>I am able to decode most of the information as stated in the Manual , But unable to convert one of the Data namely BIN Category.</p>

<p>Sample Data :</p>

<pre><code>018022
C@A@@@@@@@
Q
O
A
A
019022
CA@A@@@@@@
</code></pre>

<p>Tool Manual :</p>

<p><a href="https://i.stack.imgur.com/0mVZt.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/0mVZt.jpg" alt="Manual on How to read Incoming Data"></a></p>

<p>The data i am interetsed in are "C@A@@@@@@@" and "CA@A@@@@@@"
The First byte namely "C" is the command which is passed in.
The Second Byte which can be "@" or "A" actually tells if the Test is Pass/Fail.</p>

<p>converting to binary :</p>

<pre><code>@ ---&gt; 0100 0000
A ---&gt; 0100 0001
</code></pre>

<p>The Result is decided by the lower 4 bits of the Byte which is 0000(Pass) and 0001 (Fail). I am able to decode it correctly until here .</p>

<p>The next 8 Bytes represent the BIN category , which during test was set as 5 if the test fail and 1 if it is Pass  , so BIN number in "C@A@@@@@@@" is 1 and BIN number corresponding to "CA@A@@@@@@" is set at 5.</p>

<p>I am unable to decode the value of 5 and 1 from the data which is generated from GPIB adaptor. Can someone suggest if it can actually be decoded as 5 and 1. I have attached the Manual which explain on how to read incoming Data.</p>

<p>Stuck with this for long time :-(</p>

<p>You can use struct.unpack to decode byte values to numbers. You need to know the length (in this case 8 bytes) and whether the number is big or little endian (test if you don't know).  And whether the number is signed or unsigned. </p>

<p>If your string is  "C@A@@@@@@@" and the binary data is in bytes 3-10, you could try</p>

<pre><code>import struct    
foo="C@A@@@@@@@"
print struct.unpack("&gt;Q", foo[3:11])
</code></pre>

<p>This would decode a 8 bytes long unsigned big-endian number. See <a href="https://docs.python.org/2/library/struct.html" rel="nofollow">https://docs.python.org/2/library/struct.html</a> for instructions. </p>

<p>Hope this helps. </p>

<p>Hannu</p>

python byte decoding bits gpib