How to convert negative hexadecimal to decimal

hi I want to know how you can convert a hexadecimal negative value (to complement the encoding) to decimal, easily, without converting the hexadecimal to binary, and then multiplying each bit by a power of 2 and summing the entire value to get the result, it takes too much time: example number (32 bit): 0xFFFFFE58

so how can i do this?

+6
source share
2 answers

without using a computer, you can calculate it as follows:

0xFFFF FE58 = - 0x1A8 = -(1 * 16² + 10 * 16 + 8) = -(256 + 160 + 8) = -424 

0xFFFF FE58 - a negative number in 2 additions. To get the absolute value, you have to invert all the bits and add 1 in binary format. You can also subtract this number from the first number outside the range (0x1 0000 0000)

  0x100000000 -0x0FFFFFE58 = 0x0000001A8 

Now we know that your number is -0x1A8 . now you need to add the numbers multiplied by their place value. 8 * 16 ^ 0 + A (which is 10) * 16 ^ 1 + 1 * 16 ^ 2 = 424. Thus, the decimal value of your number is -424.

+4
source

do the calculation on a positive number, then convert to negative with two additions.

see explanation here for positive conversion from hex to decimal:

http://www.permadi.com/tutorial/numHexToDec/

mostly

  • Get the correct hexadecimal digit, name this digit currentDigit.
  • Make a variable, let it call it. Set the value to 0.
  • Multiply the current digit by (16 ^ power), save the result.
  • Power increase by 1.
  • Set currentDigit to the previous digit of the hexadecimal number.
  • Repeat from step 3 until all numbers are multiplied.
  • Summarize the result of step 3 to get the response number.
0
source

Source: https://habr.com/ru/post/974823/


All Articles