Hex to Decimal

Signed hex to decimal converter

Convert hex to a signed integer (int) in one step. Pick a bit width and type hex or a negative decimal, and the sign bit, the unsigned value and the signed value update together.

Sign bit and weights

Unsigned
Signed
Sign bit
Inverted + 1

How two's complement works

Two's complement stores negative integers by giving the top bit a negative weight equal to −2ⁿ⁻¹, where n is the bit width. Every other bit keeps its normal positive weight. The system lets hardware add positive and negative numbers with the same circuit, which is why nearly all modern processors use it.

In an 8-bit byte the top bit is worth −128 instead of +128. The remaining 7 bits still represent 0 to 127. Add those two parts and the byte covers −128 to 127.

  • 0x00 = 0. No bits set.
  • 0x7F = 127. All positive bits set, sign bit clear.
  • 0x80 = −128. Only the sign bit is set.
  • 0xFF = −128 + 127 = −1. Every bit is set.

A hex digit from 8 to F in the top position means the sign bit is 1 and the value is negative. A digit from 0 to 7 means the sign bit is 0 and the value is positive or zero. Flip the bits in the picture to see the sum change.

How to convert signed hex to decimal

Check the sign bit first. If it is 0, convert as a normal unsigned hex number. If it is 1, subtract 2ⁿ from the unsigned result, or invert the bits and add 1.

  1. Fix the bit width. Each width has a set number of hex digits: 2 for 8 bits, 4 for 16 bits, 8 for 32 bits and 16 for 64 bits.
  2. Pad the hex value with leading zeros until it fills the width. FF in 16 bits becomes 00FF, which has a 0 sign bit and reads as 255, not −1.
  3. Check the first hex digit. Digits 8 through F mean the sign bit is 1.
  4. For a positive value (sign bit 0), convert hex to decimal as normal.
  5. For a negative value (sign bit 1), use either of the two methods below.

Method 1: subtract 2ⁿ

Convert the hex to its unsigned decimal value and subtract 2ⁿ, where n is the bit width.

FF9C in 16 bits
FF9C unsigned  = 65,436
Sign bit is 1  → negative
65,436 − 2¹⁶  = 65,436 − 65,536
Signed value   = −100

Method 2: invert and add 1

Flip every bit (bitwise NOT), add 1 to the result, convert that to decimal and place a minus sign in front.

FF9C in 16 bits
FF9C  in binary = 1111 1111 1001 1100
Invert all bits = 0000 0000 0110 0011  (0x0063)
Add 1           = 0000 0000 0110 0100  (0x0064)
0x0064          = 100
Signed value    = −100

Both methods give the same answer. Method 1 is faster for mental arithmetic. Method 2 shows the relationship between a negative number and its positive counterpart.

More examples

0x80 in 8 bits
80 unsigned = 128
128 − 2⁸   = 128 − 256
Signed      = −128  (the minimum 8-bit signed value)

0xFFFFFFFE in 32 bits
FFFFFFFE unsigned = 4,294,967,294
4,294,967,294 − 2³² = 4,294,967,294 − 4,294,967,296
Signed = −2

0x7FFF in 16 bits
7FFF: first digit is 7, sign bit is 0
Convert as unsigned: 32,767
Signed = 32,767  (the maximum 16-bit signed value)

Signed and unsigned ranges

The sign bit splits the range in half. Unsigned values run from 0 to 2ⁿ − 1. Signed values run from −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1, which shifts the upper half of the unsigned range into negative numbers.

The minimum is always one larger in absolute value than the maximum: 8-bit signed holds −128 but only +127. This asymmetry exists because zero takes one slot from the positive side.

WidthHex digitsSigned minSigned maxUnsigned max
8-bit2−128127255
16-bit4−32,76832,76765,535
32-bit8−2,147,483,6482,147,483,6474,294,967,295
64-bit16−9,223,372,036,854,775,8089,223,372,036,854,775,80718,446,744,073,709,551,615

Sign extension

Sign extension widens a signed value to a larger bit width by copying the sign bit into every new upper bit. The decimal value stays the same while the hex representation changes.

FF as an 8-bit signed value is −1. To store −1 in 16 bits, copy the sign bit (1) into the 8 new upper bits: FF becomes FFFF. In 32 bits it becomes FFFFFFFF. All three hex strings mean −1 at their respective widths. A positive value extends with zeros: 7F (8-bit, +127) becomes 007F in 16 bits and 0000007F in 32 bits.

Zero extension fills the upper bits with zeros regardless of the sign bit. It treats the original bits as unsigned. FF zero-extended to 16 bits is 00FF (255), not FFFF (−1). Mixing up sign extension and zero extension is a common source of bugs when you cast between integer types in C and C++.

Value8-bit16-bit32-bit
−1FFFFFFFFFFFFFF
−1009CFF9CFFFFFF9C
−12880FF80FFFFFF80
+101000100000001
+1277F007F0000007F

Integer overflow and wraparound

Adding 1 to the largest positive signed value wraps around to the most negative value. The hex increments normally, but the signed interpretation jumps from the positive extreme to the negative extreme.

8-bit:   7F + 01 = 80       (+127 + 1 = −128)
16-bit:  7FFF + 0001 = 8000 (+32,767 + 1 = −32,768)
32-bit:  7FFFFFFF + 1 = 80000000 (+2,147,483,647 + 1 = −2,147,483,648)

The reverse also happens: subtracting 1 from the most negative value wraps to the most positive value. 0x80 − 1 = 0x7F in 8 bits, which jumps from −128 to +127.

C and C++ leave signed overflow undefined, so the compiler may optimise it away or produce unexpected results. Java wraps silently. Python integers have no fixed width, so they never overflow.

Common signed hex values

These hex values appear often in debugging, error handling and system programming. Pick one to see its bits and both readings.

HexWidthSignedWhere it appears
FF8-bit−1General error return in byte-sized APIs
808-bit−128Minimum int8 value
7F8-bit127Maximum int8 value
FFFF16-bit−1Error return in 16-bit APIs
800016-bit−32,768Minimum int16, audio sample floor
FFFFFFFF32-bit−1INVALID_HANDLE_VALUE in Windows, (size_t)−1
8000000032-bit−2,147,483,648Minimum int32 (INT_MIN in C)
7FFFFFFF32-bit2,147,483,647Maximum int32 (INT_MAX in C)
DEADBEEF32-bit−559,038,737Debug fill pattern for freed memory
CCCCCCCC32-bit−858,993,460MSVC debug fill for uninitialised stack

Signed hex in programming

Converting hex to a signed integer needs different code than unsigned conversion, because most hex-parsing functions return unsigned or arbitrary-precision values by default. Type a hex value: the width follows the number of digits.

Python

Python integers have unlimited width, so int("FF", 16) returns 255, not −1. Use bit arithmetic to apply a fixed width.

def signed(hex_str, bits):
    n = int(hex_str, 16)
    if n >= (1 << (bits - 1)):
        n -= (1 << bits)
    return n

signed("FF9C", 16)   # -100
signed("FF", 8)      # -1
signed("FF", 16)     # 255 (00FF in 16 bits, sign bit is 0)

JavaScript

parseInt returns a standard number. For 32-bit signed conversion, write the value into an Int32Array, or use a bitwise OR with 0, which forces 32-bit signed interpretation.

(parseInt("FFFFFFFF", 16) | 0)         // -1
new Int32Array([0xFFFFFFFF])[0]        // -1
new Int8Array([0xFF])[0]               // -1

C and C++

strtol and std::stoi return signed types by default, but only within the range of their type. For explicit control, cast after parsing.

int32_t n = (int32_t)strtoul("FFFFFFFE", NULL, 16);  // -2
int16_t m = (int16_t)strtoul("FF9C", NULL, 16);       // -100

Java

Integer.parseUnsignedInt (Java 8 and later) parses the full 32-bit range, and a cast to int gives the signed value.

(int) Integer.parseUnsignedInt("FFFFFFFF", 16)   // -1
(short) Integer.parseInt("FF9C", 16)              // -100
LanguageSigned 32-bit from hexKey detail
Pythonint(h, 16) - (1 << 32) if the top bit is setIntegers have no fixed width
JavaScriptparseInt(h, 16) | 0Bitwise OR forces int32
C / C++(int32_t)strtoul(h, NULL, 16)Cast after the unsigned parse
Java(int) Integer.parseUnsignedInt(h, 16)parseInt rejects values above 7FFFFFFF

Frequently asked questions

What is the difference between signed and unsigned hex?
The hex string is the same; the interpretation differs. 0xFF is 255 as an unsigned 8-bit value and −1 as a signed 8-bit value. The sign bit, the topmost bit, decides whether the number is positive or negative.
Why does bit width matter for signed hex?
The bit width determines which bit is the sign bit. FF in 8 bits has its sign bit set (−1), but FF in 16 bits is 00FF, with the sign bit clear (255). Always match the width to the data type that produced the hex.
How do you convert negative decimal to hex?
Add 2ⁿ to the negative number and convert the result to hex. For −100 in 16 bits: −100 + 65,536 = 65,436, and 65,436 in hex is FF9C. The converter accepts negative decimal input directly.
What is 0xFFFFFFFF as a signed value?
0xFFFFFFFF is −1 as a signed 32-bit integer. All 32 bits are set. Inverting gives 00000000, adding 1 gives 00000001, so the magnitude is 1 and the value is −1. Many system APIs return this value to signal an error or an invalid handle.
Does two's complement work in all programming languages?
All mainstream languages store signed integers in two's complement. The C23 and C++20 standards require it. Python, Java and JavaScript also use two's complement for their fixed-width integer operations, although Python's default integers have no fixed width.
How do you convert a signed 64-bit hex value?
Use the same method: check the sign bit at position 63, and subtract 2⁶⁴ if it is set. The hex value has 16 digits. FFFFFFFFFFFFFFFE unsigned is 18,446,744,073,709,551,614, and 18,446,744,073,709,551,614 − 2⁶⁴ = −2. In Python, use int(h, 16) - (1 << 64) for negative values.
What is integer overflow?
Integer overflow occurs when an arithmetic result exceeds the range of its type. In signed two's complement, adding 1 to the maximum positive value (0x7F in 8 bits) produces the minimum negative value (0x80, which is −128). The bits wrap around because there is no room for a larger value.
Is one's complement the same as two's complement?
No. One's complement only inverts the bits, while two's complement inverts and adds 1. One's complement has two representations of zero (+0 and −0) and a symmetric range (−127 to +127 in 8 bits). Two's complement has one zero and an asymmetric range (−128 to +127). Modern hardware uses two's complement exclusively.