Thread: Hex question

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Hex question

    I know this is probably a stupid question...

    x636F 6465 => Hex number

    Can I look at each number/letter individually and write the corresponding decimal value? Since F is the only value which won't be identical, can I just insert 15 (it's corresponding decimal value) and have the number in decimal form? I want to convert it into binary....

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by John_L View Post
    I know this is probably a stupid question...
    Not stupid - but perhaps poorly phrased.

    x636F 6465 => Hex number

    Can I look at each number/letter individually and write the corresponding decimal value?
    You can do anything you want. What's your overall objective?

    Since F is the only value which won't be identical,...
    Huh? Not sure I understand this. If I follow your logic, why isn't the 3 also unique???

    ...can I just insert 15 (it's corresponding decimal value) and have the number in decimal form?
    Well, F is 15, if it's in the lowest nibble.

    I want to convert it into binary....
    Then all you need are bit patterns to replace the values. A little tight loop that spits out a 1 or a 0 based on the result of a BITWISE AND should work nicely. Test, output the 1 or 0, shift, repeat.

    Todd

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    119
    What about negative numbers if I were to use two's complement? -1 or -432 in hexadecimal form? I have the algorithm down for positive decimal numbers to hex and binary...not sure about the negative ones for hex though (binary is easy).

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    What do you care about negative numbers when converting from a base 16 representation of a base 2 number system?

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Here's a way to convert a negative decimal number to a two-complement's hexadecimal representation
    Code:
     -1
    =   < take the absolute value of -1, which is 1 >
     1
    =   < transform it in hexadecimal >
     0x0001  (let's say we are working with 16-bits words)
    =   < negate (one complement or 15 complement if you are staying in base 16) >
     0xFFFE
    =   < add 1>
     0xFFFF
    Example with another number
    Code:
     -473
    =
     473
    =
     0x01D9
    =
     0xFE26
    =
     0xFE27

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by John_L View Post
    I know this is probably a stupid question...

    x636F 6465 => Hex number

    Can I look at each number/letter individually and write the corresponding decimal value? Since F is the only value which won't be identical, can I just insert 15 (it's corresponding decimal value) and have the number in decimal form? I want to convert it into binary....
    What does decimal have to do with anything? Anyway, hex to binary is easy enough to do in your head: 636F6465 = 01100011011011110110010001100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input a Hex number and output hex number to a text field
    By zoobaby in forum C++ Programming
    Replies: 4
    Last Post: 05-12-2009, 11:26 AM
  2. Displaying a whole file in hex
    By samus250 in forum C Programming
    Replies: 4
    Last Post: 06-30-2008, 03:31 PM
  3. Replies: 18
    Last Post: 03-26-2008, 09:01 AM
  4. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM