Skip to content Skip to sidebar Skip to footer

Convert Ascii Character To Signed 8-bit Integer Python

This feels like it should be very simple, but I haven't been able to find an answer.. In a python script I am reading in data from a USB device (x and y movements of a USB mouse).

Solution 1:

Subtract 256 if over 127:

unsigned = ord(character)
signed = unsigned - 256ifunsigned > 127elseunsigned

Alternatively, repack the byte with the struct module:

from struct import pack, unpack
signed = unpack('B', pack('b', unsigned))[0]

or directly from the character:

signed = unpack('B', character)[0]

Solution 2:

from ctypes importc_int8value= c_int8(191).value

use ctypes with your ord() value - should be -65 in this case

ex. from string data

from ctypes import c_int8
data ='BF'
value1 = int(data, 16) # or ord(data.decode('hex'))
value2 = c_int8(value1).value

value1 is 16bit integer representation of hex 'BF' and value2 is 8bit representation

Solution 3:

I know this is an old question, but I haven't found a satisfying answer elsewhere.

You can use the array module (with the extra convenience that it converts complete buffers):

fromarray import array

buf = b'\x00\x01\xff\xfe'
print(array('b', buf))

# result: array('b', [0, 1, -1, -2])

Solution 4:

Use this function to get a signed 8bit integer value

defto8bitSigned(num): 
    mask7 = 128#Check 8th bit ~ 2^8
    mask2s = 127# Keep first 7 bitsif (mask7 & num == 128): #Check Sign (8th bit)
        num = -((~int(num) + 1) & mask2s) #2's complementreturn num

Solution 5:

To convert any input bytes into signed integers:

defsigned8bit_to_int(input):
    (((input >> 7) * 128) ^ input) - ((input >> 7) * 128)

Examples:
signed8bit_to_int(0xc0) = -64
signed8bit_to_int(0xbf) = -65
signed8bit_to_int(0x0f) = 15

Explanation with 0xC0 as an example:

  • 0xc0 '0b1100 0000', its last bit is a 1, meaning it is a signed byte
  • step 1: test for signed bit: (((input >> 7) * 128)
  • step 2: if it is a signed bit, invert the input bites: from: 100 0000 to 0111 1111 (63 of base 10)
  • step 3: convert the above by substantiating 128: 63 - 128 = -65

Post a Comment for "Convert Ascii Character To Signed 8-bit Integer Python"