ThisCalculator

Everyday

Signed Hex to Decimal Calculator: How It Works

Updated · Standard formula, shown below

The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.

Signed Hex to Decimal Calculator: How It Works

This calculator converts a signed hexadecimal number (entered as a string of hex digits) into its decimal equivalent, using either two’s complement, sign-and-magnitude, or one’s complement representation, depending on the bit width you select. You enter the hex value, choose the number of bits (8, 16, 32, or 64), and pick the signed format; the calculator returns the decimal value, the unsigned decimal value, and the binary representation. The conversion follows the standard computer arithmetic rules, so you can verify the result by hand or use it to check your own code.

Formula

For a signed hex number with n bits, the decimal value depends on the format:

  • Two’s complement: If the most significant bit (MSB) is 0, the value is the same as unsigned. If MSB is 1, subtract 2n from the unsigned value. Mathematically: value = unsigned - (MSB ? 2n : 0).
  • Sign-magnitude: The MSB is the sign (1 = negative). The remaining bits give the magnitude. So value = (MSB ? -1 : 1) * (unsigned & (2n-1-1)).
  • One’s complement: Negative numbers are the bitwise NOT of the positive number. value = (MSB ? -1 : 1) * ((2n-1-1) - (unsigned & (2n-1-1))).

Here, unsigned is the decimal value of the hex string treated as a plain positive binary number. The bit width n determines the range: for two’s complement, it’s -2n-1 to 2n-1-1; for sign-magnitude and one’s complement, it’s -(2n-1-1) to 2n-1-1.

Worked Example

Using the default values: hex = FF, bits = 8, format = two’s complement.

  1. Convert hex FF to unsigned decimal: F = 15, so FF = 15×16 + 15 = 255.
  2. Bit width is 8, so 28 = 256.
  3. MSB of FF (binary 11111111) is 1, so subtract 256: 255 – 256 = -1.
  4. Result: -1.

The calculator shows -1 as the signed decimal, 255 as the unsigned decimal, and 11111111 as the binary.

When to Use Signed Hex Conversion

Signed hex conversion is essential in low-level programming, debugging memory dumps, and reverse engineering. For example, when you read a register value from a sensor or a microcontroller, it might be stored as a signed 16-bit integer in two’s complement. If the hex dump shows FFFE, you need to know whether that means 65534 or -2. The format and bit width are usually specified in the datasheet or the code that produced the value.

In networking, IP header fields and protocol flags are often displayed in hex, but some fields (like the window size) are unsigned. Signed conversion only applies when the data type is explicitly signed. In file formats, integers are often stored in little-endian or big-endian order; you must convert the byte order first, then apply the signed interpretation.

Common Mistakes and How to Avoid Them

  • Ignoring bit width: The same hex string gives different signed values for different bit widths. For example, FF is -1 in 8-bit, but 255 in 16-bit (since the MSB is 0). Always know the intended bit width.
  • Mixing up endianness: If the bytes are stored little-endian, you must reverse the byte order before conversion. For instance, the 16-bit value 0x1234 stored as bytes 34 12 becomes 0x1234 only after reordering.
  • Using the wrong signed format: Most modern systems use two’s complement, but some older hardware or protocols use sign-magnitude or one’s complement. Verify the format from the documentation.
  • Forgetting the sign extension: When converting a smaller signed value to a larger bit width, you must sign-extend (copy the MSB into the new high bits). For example, 8-bit 0x80 (-128) becomes 16-bit 0xFF80 (-128), not 0x0080 (128).

How to Choose the Right Bit Width and Format

The bit width is determined by the data type in your program or the hardware register size. Common widths are 8, 16, 32, and 64 bits. If you’re not sure, look at the number of hex digits: each hex digit represents 4 bits, so 2 hex digits = 8 bits, 4 hex digits = 16 bits, 8 hex digits = 32 bits, and 16 hex digits = 64 bits. However, the value might be padded with leading zeros, so the number of digits alone isn’t always reliable.

The signed format is almost always two’s complement in modern computing. If you’re dealing with legacy systems or specific standards, check the documentation. For example, the TCP/IP checksum uses one’s complement, but that’s a checksum, not a data value. In practice, you’ll rarely need sign-magnitude except in some floating-point representations or specialized hardware.

Limitations of the Conversion

This calculator assumes the input is a valid hex string with no spaces or prefixes like 0x. It also assumes the value fits within the chosen bit width; if the hex string has more digits than the bit width, the calculator will truncate the leading digits (or you should manually truncate). For example, if you enter 1FF with 8 bits, the calculator will only consider the lower 8 bits (FF) and ignore the leading 1. That’s a common source of errors, so always ensure the input length matches the bit width.

The conversion itself is exact for integers; there is no rounding. However, if you’re converting a hex value that represents a floating-point number (like IEEE 754), this calculator is not appropriate because the bit pattern is interpreted differently. For floating-point, you’d need a separate tool that decodes the sign, exponent, and mantissa.

Frequently Asked Questions

What is two’s complement and why is it used?

Two’s complement is a method of representing signed integers in binary where the most significant bit indicates the sign. To get the negative of a number, invert all bits and add 1. It’s used because addition and subtraction work the same for signed and unsigned numbers, simplifying hardware.

How do I convert a signed hex to decimal manually?

First, convert the hex to unsigned decimal. Then, if the MSB (the leftmost bit) is 1, subtract 2n (where n is the bit width) from that value. For example, 8-bit F0 is 240 unsigned, minus 256 = -16.

What is the range of a signed 8-bit number?

For two’s complement, the range is -128 to 127. For sign-magnitude and one’s complement, the range is -127 to 127 (because there are two representations for zero).

Why does 0xFFFFFFFF equal -1 in 32-bit?

Because in 32-bit two’s complement, the unsigned value is 4,294,967,295. Subtracting 232 (4,294,967,296) gives -1. This is the same as -1 in 8-bit 0xFF.

Can I use this calculator for 64-bit values?

Yes, select 64-bit width. The calculator handles up to 16 hex digits, but be aware that JavaScript numbers can only represent integers exactly up to 2^53, so for 64-bit values the result may be approximate for very large magnitudes. For precise 64-bit conversion, use a language with arbitrary-precision integers.

What is the difference between signed and unsigned hex?

Unsigned hex always represents a non-negative integer. Signed hex uses the same bit pattern but interprets the MSB as a sign or as part of a two’s complement representation, allowing negative values. The same hex string can mean different numbers depending on whether it’s signed or unsigned.

Related

Formula used
in hex | Hex value | - | FF | 0
sel bits | Bit width | 8=8, 16=16, 32=32, 64=64
sel format | Signed format | Two's complement=2, Sign-magnitude=1, One's complement=3
out signed_dec | Signed decimal | - | ( ( (hex_to_dec(hex) + (1  (bits-1) ? -1 : 1) * ( ( (1

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.