Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to convert negative hexadecimal to decimal

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

so how can I do it?

like image 867
KarimS Avatar asked Sep 04 '14 11:09

KarimS


People also ask

How do you convert negative to hexadecimal?

The hexadecimal value of a negative decimal number can be obtained starting from the binary value of that decimal number positive value. The binary value needs to be negated and then, to add 1. The result (converted to hex) represents the hex value of the respective negative decimal number.

How do I convert hexadecimal to decimal?

Given hexadecimal number is 7CF. To convert this into a decimal number system, multiply each digit with the powers of 16 starting from units place of the number. From this, the rule can be defined for the conversion from hex numbers to decimal numbers.

What is FFFF hex in decimal?

My book says the hexadecimal notation FFFF equals 65535 in decimal value.

How do you convert a negative decimal number to binary?

The simplest is to simply use the leftmost digit of the number as a special value to represent the sign of the number: 0 = positive, 1 = negative. For example, a value of positive 12 (decimal) would be written as 01100 in binary, but negative 12 (decimal) would be written as 11100.


1 Answers

without using a computer you can calculate it like this:

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

0xFFFF FE58 is a negative number in 2's complement. To get the absolute value you have to invert all bits and add 1 in binary. You also can subtract this number from the first number out of range (0x1 0000 0000)

 0x100000000
-0x0FFFFFE58
      =
 0x0000001A8

now we know that your number is -0x1A8. now you have to add up the digits multiplied with their place value. 8 * 16^0 + A (which is 10) * 16^1 + 1 * 16^2 = 424. So the decimal value of your number is -424.

like image 146
mch Avatar answered Sep 24 '22 01:09

mch