Subtract 256 if over 127:

unsigned = ord(character)
signed = unsigned - 256 if unsigned > 127 else unsigned

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]
Answer from Martijn Pieters on Stack Overflow
Discussions

ctypes - Convert a string to an 8-bit signed integer in python - Stack Overflow
I'm trying to patch together a motor control system using python and ctypes and one of the things I need to do is take an text input and convert it to an 8-bit signed integer. Below is the documen... More on stackoverflow.com
🌐 stackoverflow.com
A 1 byte (8 bit) value is treating what is typically an unsigned value as a signed value and creating an inaccurate number. How do I fix this?
How are you receiving these values? Please provide some details on the source and format of the data. Please be specific on what the raw data looks like, if you print/write the data to a terminal/file what does it look like (almost certainly not the string representation of binary number i.e. 0bxxxx...), where the data is coming from (Serial interface? File? Raw bytes or text?) and what type of data these bytes are meant to represent. If the data you are working with literally is a string (a word or a sequence of characters like in a text file) "0b11000011" then your data is actually a sequence of bytes (possibly multiple per character but usually not). It's important to understand if you are working with raw bytes or text. More on reddit.com
🌐 r/learnpython
11
4
June 27, 2024
Simulating C#'s sbyte (8 bit signed integer) casting in Python - Stack Overflow
In C#, I can cast things to 8bit signed ints like so: (sbyte)arg1; which when arg1 = 2, the cast returns 2 also. However, obviously casting 128 will return -128. More specifically casting 251 will More on stackoverflow.com
🌐 stackoverflow.com
Declaring an Unsigned Integer 16 in Python for Bit Shift Operation
I would just do this: dy = (dx << 8) & 0xFFFF The 0xFFFF is 16 1's, so when you do a bitwise AND everything above the 16th bit is set to 0. More on reddit.com
🌐 r/learnpython
10
1
October 29, 2023
🌐
Quora
quora.com › How-can-I-compress-a-16-bit-integer-to-an-8-bit-size-integer-in-Python
How to compress a 16-bit integer to an 8-bit size integer in Python - Quora
As Tony Flury points out, though, ... to do in Python - you would be better off trying it in C, which is well suited to working with bits at a low level. Is it worth doing? For one single number, it isn’t - you would save 8 bits, and probably use up several hundred bytes (at least) on the compression code, even using C. If you had a large number of small values it might be worth doing. ... Compressing a 16-bit signed or unsigned integer into 8 bits ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-convert-signed-to-unsigned-integer-in-python
How to convert signed to unsigned integer in Python ? - GeeksforGeeks
4 days ago - Python provides a single int type for integers and does not have separate signed and unsigned integer data types. Unlike languages such as C and C++, Python integers are not restricted to 32 bits.
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-signed-to-unsigned-integer-in-python
How to Convert Signed to Unsigned Integer in Python?
July 24, 2023 - 8-bit conversion: -1 ? 255 -128 ? 128 16-bit conversion: -1 ? 65535 -32768 ? 32768 · Converting signed to unsigned integers in Python involves shifting negative values into the positive range using addition or bitwise operations.
Find elsewhere
🌐
LabEx
labex.io › tutorials › python-how-to-work-with-signed-binary-numbers-462163
How to work with signed binary numbers | LabEx
Two's complement is the standard method for representing signed integers in most modern computer systems. ... def twos_complement(number, bits=8): """Convert number to two's complement representation""" if number < 0: number = (1 << bits) + ...
🌐
Python Data Science Handbook
jakevdp.github.io › PythonDataScienceHandbook › 02.01-understanding-data-types.html
Understanding Data Types in Python | Python Data Science Handbook
Notice the difference here: a C integer is essentially a label for a position in memory whose bytes encode an integer value. A Python integer is a pointer to a position in memory containing all the Python object information, including the bytes that contain the integer value.
🌐
Real Python
realpython.com › ref › computer-science-glossary › signed-integer
signed integer | Computer Science Glossary – Real Python
The most significant bit carries a negative weight, so an n-bit value spans the asymmetric range from -2ⁿ⁻¹ to 2ⁿ⁻¹ - 1. An 8-bit signed integer therefore runs from -128 to 127.
🌐
Hexcalculator
hexcalculator.org › hex-to-signed-integer
Hex to Signed Integer Converter Online
May 29, 2026 - This format is widely used in ... number to a signed integer involves interpreting the hex as binary and determining if the most significant bit (MSB) represents a negative value....
🌐
Python Forum
python-forum.io › thread-21116.html
8-bit integer to bytes
September 14, 2019 - Hello... I am attempting to use a Python3 library function that requires a binary parameter (bytes or bytearray). When I submit a parameter such as b'7' the function operates properly. But, I need it to accept an arbitrary 8-bit integer that I ...
🌐
Wikitechy
wikitechy.com › interview-questions › programming › a-8-bit-signed-integer-has-the-following-range
A 8-bit signed integer has the following range ? - Programming
September 20, 2021 - There are 28 (256) different possible values for 8 bits. When unsigned integer, it has possible values ranging from 0 to 255. ... 10 bit value range128 bit integer limit16 bit integer16 bit integer max value16 bit integer range32 bit integer range32 bit signed integer range32 bit signed integer ...
🌐
Reddit
reddit.com › r/learnpython › a 1 byte (8 bit) value is treating what is typically an unsigned value as a signed value and creating an inaccurate number. how do i fix this?
r/learnpython on Reddit: A 1 byte (8 bit) value is treating what is typically an unsigned value as a signed value and creating an inaccurate number. How do I fix this?
June 27, 2024 -

A 1byte (8 bit) value is treating what is typically an unsigned value as a signed value and creating an inaccruate number. How do I fix this?

I am receiving an 8 bit number (outside of my control) and it is treating values like

0b11000011

would be

0xc3

but it's being interpreted as

-0x3d

I want it to be treated as 0xc3 and not 0x3d.

If you're thinking you're using the wrong language, you're right. I am. I don't have control over that. Second, I am using python 2. I know. It's EOL and doesn't receive security updates. I'm aware. I shouldn't. It's out of my control.

If you have guidance, let me know.

edit: would something like (but do let me know if there are any traps or pitfalls I'm inducing by doing this)

new_unsigned_value = c_uint8(signed_value).value
Top answer
1 of 2
6
How are you receiving these values? Please provide some details on the source and format of the data. Please be specific on what the raw data looks like, if you print/write the data to a terminal/file what does it look like (almost certainly not the string representation of binary number i.e. 0bxxxx...), where the data is coming from (Serial interface? File? Raw bytes or text?) and what type of data these bytes are meant to represent. If the data you are working with literally is a string (a word or a sequence of characters like in a text file) "0b11000011" then your data is actually a sequence of bytes (possibly multiple per character but usually not). It's important to understand if you are working with raw bytes or text.
2 of 2
1
Send it as a single string value, which is 8 bits. Problem solved. Python really has no individual "types" for numbers, other than absolute and floating point. Both have a sign, as they are virtually treated as an interoperable string if a numeric value, in most instances. Functions often evaluate the string into a fitting number, before use. That is why many numeric functions are slow, except in compound formulations. I'd assume a function you are passing the value to, demands a signed value, so it is forcing the value to be what it needs, from a string value. Think about looking into assistive C based math functions, if you really NEED to use explicit numeric types. Python does not have such a thing, natively. (Makes math functions faster too. Almost as fast as native C functions.)
🌐
Data-apis
data-apis.org › array-api › 2024.12 › API_specification › data_types.html
Data Types — Python array API standard 2024.12 documentation
Data type objects are used as dtype specifiers in functions and methods (e.g., zeros((2, 3), dtype=float32)), accessible as .dtype attribute on arrays, and used in various casting and introspection functions (e.g., isdtype(x.dtype, 'integral')).
🌐
Python Tutorial
pythontutorial.net › home › advanced python › python integers
An Essential Guide to Python Integers
March 27, 2025 - By using 8 bits, you can store up to 28 – 1 = 255 integers. To store both negative integers, zero, and positive integers, you need to reserve 1 bit for storing the sign, negative (-) and positive (+).
🌐
Data-apis
data-apis.org › array-api › 2023.12 › API_specification › data_types.html
Data Types — Python array API standard 2023.12 documentation
The default integer data type should be the same across platforms, but the default may vary depending on whether Python is 32-bit or 64-bit.