Originally Posted by
f.g
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:
Originally Posted by
f.g
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.