简体   繁体   English

如何将负十六进制转换为十进制

[英]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 嗨,我想知道如何将十六进制负值(到补码编码)转换为十进制,很容易,不用将十六进制转换为二进制,然后将每个位乘以2的幂,并将所有值相加得到结果,它需要太多时间:数字(32位)的例子:0xFFFFFE58

so how can I do it? 那怎么办呢?

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. 0xFFFF FE58是2的补码中的负数。 To get the absolute value you have to invert all bits and add 1 in binary. 要获得绝对值,必须反转所有位并在二进制中加1。 You also can subtract this number from the first number out of range (0x1 0000 0000) 你也可以从超出范围的第一个数字中减去这个数字(0x1 0000 0000)

 0x100000000
-0x0FFFFFE58
      =
 0x0000001A8

now we know that your number is -0x1A8 . 现在我们知道你的号码是-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. 8 * 16 ^ 0 + A(10)* 16 ^ 1 + 1 * 16 ^ 2 = 424.因此您的数字的十进制值为-424。

do a calculation on the positive number, then convert to the negative with Two's complement. 对正数进行计算,然后用二进制补码转换为负数。

see explanation here for positive conversion from hexa to decimal: 请参阅此处有关从六进制到十进制的正转换的说明:

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

basically: 基本上:

  1. Get the right most digit of the hex number, call this digit the currentDigit. 获取十六进制数字的最右边数字,将此数字称为currentDigit。
  2. Make a variable, let's call it power. 做一个变量,让它称之为力量。 Set the value to 0. 将值设置为0。
  3. Multiply the current digit with (16^power), store the result. 将当前数字乘以(16 ^幂),存储结果。
  4. Increment power by 1. 增加功率1。
  5. Set the the currentDigit to the previous digit of the hex number. 将currentDigit设置为十六进制数的前一个数字。
  6. Repeat from step 3 until all digits have been multiplied. 从步骤3开始重复,直到所有数字都成倍增加。
  7. Sum the result of step 3 to get the answer number. 将步骤3的结果相加以得到答案编号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM