2 bytes to short

Remember, you don’t have to tie yourself in knots with bit shifting if you’re not too familiar with the details. You can use a ByteBuffer to help you out: ByteBuffer bb = ByteBuffer.allocate(2); bb.order(ByteOrder.LITTLE_ENDIAN); bb.put(firstByte); bb.put(secondByte); short shortVal = bb.getShort(0); And vice versa, you can put a short, then pull out bytes. By the way, … Read more

“TypeError: a bytes-like object is required, not ‘str'” when handling file content in Python 3

You opened the file in binary mode: with open(fname, ‘rb’) as f: This means that all data read from the file is returned as bytes objects, not str. You cannot then use a string in a containment test: if ‘some-pattern’ in tmp: continue You’d have to use a bytes object to test against tmp instead: … Read more