# iterating through hex numbers in c

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 08-24-2011
f.g
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.
• 08-24-2011
tabstop
I guess the question is "why are you holding hex numbers in a string?"
• 08-24-2011
TheBigH
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.
• 08-24-2011
quzah
Quote:

Originally Posted by TheBigH
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.
• 08-24-2011
f.g
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.
• 08-24-2011
tabstop
Quote:

Originally Posted by f.g
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?
• 08-24-2011
quzah
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.
• 08-24-2011
quzah
Quote:

Originally Posted by tabstop
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.
• 08-24-2011
TheBigH
Quote:

Originally Posted by quzah
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).
• 08-24-2011
tabstop
Quote:

Originally Posted by quzah
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".
• 08-24-2011
quzah
Quote:

Originally Posted by tabstop
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.
• 08-24-2011
f.g
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).
• 08-24-2011
quzah
Quote:

Originally Posted by f.g
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.
• 08-24-2011
tabstop
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.
• 08-24-2011
quzah
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.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last