Convert str bytes to bytes <p>I have a bluetooth controller which returns the data of a event in <code>received_data</code> where the 3 &amp; 4 byte together are a 16 bit signed little-endian int value. </p> <p>With python2 where a str is also a byte I just exctract it with <code>struct.unpact</code></p> <pre><code>rotation_value = struct.unpack("&lt;h", received_data[3] + received_data[4])[0] </code></pre> <p>This doesn't work with python3 because str and byte are not the same thing</p> <pre><code>rotation_value = struct.unpack("&lt;h", received_data[3] + received_data[4])[0] TypeError: 'str' does not support the buffer interface </code></pre> <p>So my approach was to just convert the str to bytes.</p> <p>here are things I tried</p> <pre><code>print((received_data[3]+received_data[3]).encode('utf_16_le')) print(received_data[3].encode('ISO-8859-1') + received_data[4].encode('ISO-8859-1')) print(int.from_bytes(bytes([ord(c) for c in (received_data[3]+received_data[3])]), byteorder='little', signed=True)) print(struct.unpack("&lt;h", bytearray(received_data[3] + received_data[4], 'cp1252'))[0]) </code></pre> <p>But no matter how I do it I have allays error messages like these:</p> <pre><code>UnicodeDecodeError: 'utf-8' codec can't decode byte 0xef in position 3: invalid continuation byte </code></pre> <p>As <a href="http://stackoverflow.com/users/4014959/pm-2ring">@PM 2Ring</a> proposed to use latin1</p> <pre><code>print(received_data[3].encode('latin1')) </code></pre> <p>Which didn't work </p> <pre><code>UnicodeDecodeError: 'utf-8' codec can't decode byte 0xca in position 3: invalid continuation byte UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 3: invalid start byte </code></pre> <p>So what exactly is the correct way to convert these fake str in bytes, preferably a solution which works on python2 &amp; 3. </p>