Convert signed hexadecimal to integer

Convert signed hex to decimal using two’s complement. Learn why bit width matters, how to handle a minus sign, and how to check your result.

To convert signed hex to decimal, first identify how the sign is represented. An explicit negative value such as -0x2A converts directly to -42. For a two’s complement bit pattern, you need its bit width: parse the hex as an unsigned integer, then subtract 2^n if the highest bit is set, where n is the bit width.

Bit width determines the signed value

Hexadecimal is a way to write a number; it does not specify signedness or bit width by itself. Each hex digit represents four bits, but omitted leading zeros can hide the intended width. For example, FF represents -1 as an 8-bit two’s complement value, while 00FF represents 255 as a 16-bit value. If the source gives only FF without a width or sign convention, its signed interpretation is uncertain.

Convert a two’s complement hex value

  • 1. Confirm the source uses two’s complement and identify its bit width, n.
  • 2. Parse the hexadecimal digits as an unsigned integer, U.
  • 3. Check that U fits within n bits: 0 ≤ U < 2^n.
  • 4. If U < 2^(n−1), the signed decimal result is U.
  • 5. Otherwise, the signed decimal result is U − 2^n.

For example, 8-bit D6 has the unsigned value 13 × 16 + 6 = 214. The 8-bit sign threshold is 128, so subtract 256: 214 − 256 = -42. The same digits interpreted as a 16-bit value represent positive 214.

Hex         Bit width    Unsigned value    Signed decimal
7F          8            127               127
80          8            128               -128
FF          8            255               -1
00FF        16           255               255
8000        16           32768             -32768
FFFFFFFF    32           4294967295        -1

Handle an explicit minus sign separately

If the input is -0x2A, convert the magnitude 2A to 42 and apply the minus sign. This notation already expresses a negative number; do not apply the two’s complement subtraction rule to its magnitude. By contrast, a raw pattern such as D6 needs a width and an encoding convention before you can interpret its sign.

Convert hex to a signed integer in Python

Python’s int(text, 16) parses a hexadecimal number and accepts an explicit minus sign. It does not infer a fixed-width two’s complement interpretation from a high leading digit. For unsigned hex input representing a two’s complement pattern, use an explicit width:

def signed_hex_to_int(text, bits):
    if bits <= 0:
        raise ValueError("Bit width must be positive")

    text = text.strip()
    if text.startswith(("+", "-")):
        raise ValueError("Enter a bit pattern without a sign")

    value = int(text, 16)
    if not 0 <= value < (1 << bits):
        raise ValueError("Value does not fit the bit width")

    if value >= (1 << (bits - 1)):
        value -= 1 << bits
    return value

print(signed_hex_to_int("D6", 8))   # -42
print(signed_hex_to_int("FF", 16))  # 255
print(int("-0x2A", 16))            # -42

Check the source and preserve usable output

An n-bit two’s complement result must fall between -2^(n−1) and 2^(n−1) − 1. Confirm the width and encoding against the source format before relying on the result. If the input is a sequence of bytes, also establish the byte order before treating it as one number. No source format was supplied here, so a particular width, encoding or byte order cannot be assumed.

Use ordinary decimal digits and an ASCII minus sign for output intended for copying into code, forms or data files. Keep width and encoding details alongside the value when they matter. No destination-specific rendering, assistive-technology tests or external converter documentation were supplied, so compatibility and data handling remain unverified. Check the destination’s requirements and any external service’s handling policy before submitting sensitive values; use a trusted local workflow when external processing is unsuitable.