In n-bit two's complement, bits have value:

bit 0 = 20
bit 1 = 21
bit n-2 = 2n-2
bit n-1 = -2n-1

But bit n-1 has value 2n-1 when unsigned, so the number is 2n too high. Subtract 2n if bit n-1 is set:

def twos_complement(hexstr, bits):
    value = int(hexstr, 16)
    if value & (1 << (bits - 1)):
        value -= 1 << bits
    return value

print(twos_complement('FFFE', 16))
print(twos_complement('7FFF', 16))
print(twos_complement('7F', 8))
print(twos_complement('FF', 8))

Output:

-2
32767
127
-1
Answer from Mark Tolonen on Stack Overflow
🌐
LabEx
labex.io › tutorials › python-how-to-handle-hex-conversion-with-signed-numbers-435393
How to handle hex conversion with signed numbers | LabEx
## Hex in bitwise operations a = 0x0F ## Binary: 00001111 b = 0xF0 ## Binary: 11110000 print(hex(a & b)) ## Bitwise AND · Signed number encoding allows computers to represent both positive and negative numbers using various methods.
🌐
Reddit
reddit.com › r/learnpython › script to convert hex literals (0xff) to signed integers (-1)?
r/learnpython on Reddit: Script to convert hex literals (0xFF) to signed integers (-1)?
July 9, 2025 -

My company has hundreds, perhaps thousands, of test scripts written in Python. Most were written in Python 2, but they are slowly being converted to Python 3. I have found several of them that use hexadecimal literals to represent negative numbers that are to be stored in numpy int8 objects. This was OK in Python 2, where hex literals were assumed to be signed, but breaks in Python 3, where they're assumed to be unsigned.

x = int8(0xFF)
print x

prints -1 in Python 2, but in Python 3, it throws an overflow error.

So, I would like a Python script that reads through a Python script, identifies all strings beginning with "0x", and converts them to signed decimal integers. Does such a thing exist?

🌐
Post.Byes
post.bytes.com › home › forum › topic › python
hex to signed integer - Post.Byes - Bytes
July 18, 2005 - Re: hex to signed integer Quoth Tom Goulet:[color=blue] > My question basically is: What is the opposite of the following? > | "X" % -1[/color] Here's one way, very like what you already have: def hex2signed(s): return struct.unpack(' !i', binascii.unhexl ify(s))[0] (This will not be the inverse of 'x' % n in Python 2.4, when '%x' % -1 will produce '-1', but I think it does what you want.)
Find elsewhere
🌐
Python
python-list.python.narkive.com › SixIKozv › hex-to-signed-integer
hex to signed integer
I want to convert a string of hexadecimal characters to the signed integer they would have been before the <print> statement converted them. How do I do this in such a way that is compatible with Python versions 1.5.2 through 2.4, and not machine-dependent? temp = int(hexbit,16) temp = int(long(hexbit,16)-2**32) The <int> function takes only one argument in Python 1.5.2.
🌐
Python
mail.python.org › pipermail › python-list › 2003-July › 183658.html
hex to signed integer
July 18, 2003 - > > > if hexbit[0] < '8': > > temp = int(hexbit,16) > > else: > > temp = int(long(hexbit,16)-2**32) > > The <int> function takes only one argument in Python 1.5.2. Yes, the base argument was added in later around Python 2.0. An equivalent operation to int(value,base) in Python 1.5.2 and any of the later versions (at least through 2.3 - 2.4 doesn't exist yet) would be the string.atoi(value,base) function. However, as indicated by the above code, both int() and string.atoi() limit their result to a signed integer, so depending on the hexadecimal string it might overflow and result in an exception.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "python how-to's" › "how to convert hex string to int in python"
How to Convert Hex String to Int in Python | Delft Stack
February 12, 2024 - The variable hex_val is initialized with the hexadecimal value ff, which we aim to convert to a signed integer.
🌐
Hexcalculator
hexcalculator.org › hex-to-signed-integer
Hex to Signed Integer Converter Online
May 29, 2026 - Unlike unsigned integers, which only handle non-negative values, signed integers use two’s complement binary representation to store negative values. This format is widely used in programming languages like C, Python, and JavaScript, as well as in system-level applications and embedded devices. the hex to Signed integer converter image is shown below: