Thread: iterating through hex numbers in c

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    5

    iterating through hex numbers in c

    hello everyone,

    i'm trying to iterate through a hexadecimal number, which is held in a string. i was hoping to find a neater way to do this than with mechanically diong it unit by unit. is there any easier way to treat hex numbers / iterate through them incrementing with each iteration?

    sorry if this is a stupid question, but it seems that if you can format out put as hex numbers there should be an easier way to increment them.

    thanks for all and any replies.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess the question is "why are you holding hex numbers in a string?"

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Does your hexadecimal number string begin with '0x' or '0X'? If so, you can use the strtoi function to convert it to an integer. Then you can increment the integer, and use sprintf to print the incremented integer back to a string.
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by TheBigH View Post
    If so, you can use the strtoi
    That should be an l (ell) not an i (eye).

    strto*:
    d = double
    f = float
    ld = long double
    l = long
    ll = long long
    ul = unsigned long
    ull = unsigned long long
    imax - convert strings to maximum sized integers
    uimax - "


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

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    hey folkes,
    thanks for your quick replies. i guess i could use an integer, or add '0x' to the beggining of the string or something like that. basically it is passed into the function, and then to another function as a char* without a '0x' at the beginning. i guess i can turn it into an int and back, and then chop of the '0x'. i guess i'm surprised that there isn't a way to use it as it is.

  6. #6
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by quzah View Post
    That should be an l (ell) not an i (eye).

    strto*:
    d = double
    f = float
    ld = long double
    l = long
    ll = long long
    ul = unsigned long
    ull = unsigned long long
    imax - convert strings to maximum sized integers
    uimax - "


    Quzah.

    Whoops, you're right. I forgot that strtoi is the only one left out of that family of functions. (I saw it years ago somewhere, but it seems to have died).
    Code:
    while(!asleep) {
       sheep++;
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by f.g View Post
    i guess i'm surprised that there isn't a way to use it as it is.
    Probably because it is completely ludicrous. Why are you storing an integer as a string?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    Probably because it is completely ludicrous. Why are you storing an integer as a string?
    Text files. Why do people use them? Beats me, but they always show up here complaining about flat files being bad. :P


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

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Text files. Why do people use them? Beats me, but they always show up here complaining about flat files being bad. :P


    Quzah.
    And given that we're passing this integer around to various and sundry other functions with and without 0x, the answer is probably even worse: "we've always used a string".

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    5
    hey, the firmware function that this needs to be passed to it some one else's work and non-negotiable. i should perhaps also point out that this is for a atmega128 microcontroller, so that might have something to do with it (the number has to exceed the largest unsigned int that the chip can contain).

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can use it as-is. You just have to do it yourself manually. For example:
    Code:
    switch( ... )
    {
        case '0': ...
        case '1': ...
        ...
        case 'F': ...
    }
    You also have to think about what happens if the left most value is an F and you need to increment that. Do you grow your string longer, or do you treat it as overflow and apply some rule that you decide upon?


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

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    the answer is probably even worse: "we've always used a string".
    Why store something in one byte what you can store it in five!
    Code:
    char *p = "0xFF";
    unsigned char byte = 0xFF;

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

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by f.g View Post
    the number has to exceed the largest unsigned int that the chip can contain
    So you want a built in simple way to work with numbers too big to work with normally? That's pretty silly if you stop and actually think about what you are asking. You are going to need to make your own method of handling this. Like I said, you need to also decide what happens when you are trying to increment something bigger than what your string allows.


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

  14. #14
    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.

  15. #15
    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.

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, 12:38 PM
  2. Iterating over private vector
    By mrpringle in forum C++ Programming
    Replies: 9
    Last Post: 08-15-2010, 11:56 AM
  3. Iterating through a map that contains a vector
    By AngryStyro in forum C++ Programming
    Replies: 2
    Last Post: 06-08-2008, 05: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