iterating through hex numbers in c

This is a discussion on iterating through hex numbers in c within the C Programming forums, part of the General Programming Boards category; yeah, they are written to the eeprom on the chip along with the application, then operates independently once powered up. ...

  1. #16
    f.g
    f.g is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    yeah, they are written to the eeprom on the chip along with the application, then operates independently once powered up.
    so quzah, these numbers would not be too large to work with normally, just in this context, i've just not written in c for a while and thought that there might be a better way.

  2. #17
    Registered User
    Join Date
    Apr 2006
    Posts
    2,053
    Generally it's easier to use "big numbers" in assembler than C, because you can directly detect integer overflow.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by f.g View Post
    so quzah, these numbers would not be too large to work with normally, just in this context
    Right, by "bigger than what you can do normally" means "on this particular setup, it's bigger than what you can normally do". If the only thing your implementation of C had was char, then trying to manipulate a two-byte value wouldn't be something you can normally do. In your case, your have something with what, 16 bit ints? So trying to use a 32 bit value is bigger than what it can normally do. Thus, you need a string or an array of said type, or whatever you choose.

    Now, you've already said you have to use a string and you can't change that, so obviously:
    Quote Originally Posted by f.g View Post
    thought that there might be a better way.
    There isn't a better way, because you just said you had to use a string. What you could do is reverse the order that they are stored in the string, and don't bother storing 0x (or in your case x0 if it was reversed).

    "0123456789abcdef" -> "fedcba907654321"
    Code:
    for( p = string; *p; p++ )
    {
        switch( tolower( *p ) )
        {
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e': (*p)++; return;
            case '9': *p = 'a'; return;
            case 'f' : *p = '0'; /* now you are carrying, if you run out of string, you need to do something */
        }
    }
    That looks about right.


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

  4. #19
    f.g
    f.g is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    thanks eveyone, i'll try it with a swith statement tomorrow.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Deleting an object while iterating
    By nacho4d in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2010, 01:38 PM
  2. Iterating over private vector
    By mrpringle in forum C++ Programming
    Replies: 9
    Last Post: 08-15-2010, 12:56 PM
  3. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 06:01 AM
  4. iterating over arrays of pointers
    By cs32 in forum C Programming
    Replies: 4
    Last Post: 01-08-2008, 09:16 AM
  5. Iterating through an array
    By MikeyIckey in forum C Programming
    Replies: 15
    Last Post: 11-10-2007, 10:26 PM

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