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; hello everyone, i'm trying to iterate through a hexadecimal number, which is held in a string. i was hoping to ...

  1. #1
    f.g
    f.g is offline
    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,185
    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
    f.g
    f.g is offline
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

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

  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
    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++;
    }

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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".

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

  12. #12
    f.g
    f.g is offline
    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).

  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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Are you writing the firmware/on the chip?

    If not, then the firmware/chip limitations are completely irrelevant to you (apart from I/O). If you have to send it as a string, then send it as a string, but at no other time should this limitation enter your mind.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if string is all Fs
        realloc one more slot
        set the left most spot to '1' and the rest to '0'
    else
        walk from the right most back to the left most, incrementing until you don't need to carry a number

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

Page 1 of 2 12 LastLast
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