Thread: Converting Two's Complement from Hex to Decimal

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    35

    Converting Two's Complement from Hex to Decimal

    I don't understand how to do this. I understand how to convert from Hex to Decimal, but not specifically with Two's Complement form. I need to convert 0xfffffe58 into decimal form. I get that the first character here indicates that the number is negative, and I know that the decimal equivalent is -424. I tried these approaches:

    -15 * 16^7 + 15*(16^6 + 16^5 + 16^4 + 16^3) + 14*16^2 + 5*16 + 8

    and

    -15*16^3 + 14*16^2 + 5*16 + 8

    I also thought that it might be correct to just do this:

    (14*16^2 + 5*16 + 8 + 1) * -1

    None of these are right, and I can't find out how to do this anywhere. Can anybody help? Thanks!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Subtract 0xfffffe58 from 0xffffffff to get 0x000001a7, also known as 423, then add 1 and tack the minus sign on.

    (EDIT: Or I suppose you can do it the "proper" way, which is to subtract 0x100000000, since that is what the word "two's complement" means, to take it away from a power of two.)
    Last edited by tabstop; 08-28-2011 at 04:54 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should probably start here: Two's complement - Wikipedia, the free encyclopedia


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Subtract 1 from the hex number and take its 1's complement.
    But as noted, it's vital to understand 2's complement notation.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from 2's complement to decimal integer
    By clancyPC in forum C Programming
    Replies: 5
    Last Post: 05-05-2011, 08:51 AM
  2. Converting from ternary to decimal
    By kgehrma in forum C Programming
    Replies: 19
    Last Post: 02-08-2011, 05:29 PM
  3. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM
  4. Converting decimal to binary?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-17-2002, 08:21 AM