about hexa-decimal numbers

This is a discussion on about hexa-decimal numbers within the C++ Programming forums, part of the General Programming Boards category; hello all. i have this problem. i have a file that is full of numbers, i want to take these ...

  1. #1
    Unregistered
    Guest

    Question about hexa-decimal numbers

    hello all.

    i have this problem. i have a file that is full of numbers, i want
    to take these numbers (two at a time) and treat them as
    hexadecimal.

    for example, a file that contains the numbers

    534545245237

    i want to make that :

    0x53 0x45 0x45 0x24 0x52 0x37

    and i want to check the bits of them one by one :

    0x53 : 01010011 // ((if bit0==1){dothat();} if(bit2==0){donot();}

    etc...

    is there a way?

    thanks a lot.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    68

    There is a way

    hi U,

    What u can do is following

    -- Create and array of some size,
    -- open the file and read two char or int at a time
    -- then add 0x to the front
    -- story it into the array....

    Thanks

  3. #3
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    read through one character at a time. the ones column will be added to the other column times 16. ie:
    54 = 4 + (5 * 16)
    more technically
    54 = (4 * 16^0) + (5 * 16^1)

    by now you should have a hex number you want to test for bits. 1 looks like this:
    Code:
    0000 0001
    if you want to test the ones digit here you want to mask away all the unnecessary digits. 0x54 looks like this:
    Code:
    0101 0100
    to test a bit you want to logical and it. in c, it's this sign: &
    Code:
    0101 0100
    &
    0000 0001
    =
    0000 0000
    and returns a one only when both are ones.
    you want to set up this kind of loop:
    Code:
    int x = 0x57;
    for (int i=0x80;i;i>>=1)
      if (x & i)
        printf("1");
      else
        printf("0");
    the i>>=1 shifts i's bit once to the right.
    i starts off as
    Code:
    1000 0000
    0100 0000
    0010 0000
    0001 0000
    0000 1000
    0000 0100
    0000 0010
    0000 0001
    every time you test the other number to see if the bit is set, then do something accordingly.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    68
    Nice

  5. #5
    Unregistered
    Guest
    hello and thanks for the answers,

    ygfperson, in order to make the characters numbers, i must
    use the atoi function. but what will happen if i have letters?
    e.g. 0x5a?

    thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:14 AM
  3. Confused by expression.
    By Hulag in forum C Programming
    Replies: 3
    Last Post: 04-07-2005, 07:52 AM
  4. Help needed with VERY large numbers.
    By jwarner in forum C++ Programming
    Replies: 4
    Last Post: 01-18-2004, 11:01 AM
  5. decimal to binary, decimal to hexadecimal and vice versa
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 12-08-2001, 10:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21