theotyflos
Programmer
Hi all,
having a string containing 4 hex digits, I need a function that returns the next hex value excluding 'A's. For example:
string = "1999" ==> next value = "199B". This is what i have so far:
It works, but since I'm not a C expert, I would appreciate any comments and/or improvements.
Thanks all in advance.
Theophilos.
-----------
There are only 10 kinds of people: Those who understand binary and those who don't.
having a string containing 4 hex digits, I need a function that returns the next hex value excluding 'A's. For example:
string = "1999" ==> next value = "199B". This is what i have so far:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* returns the next hex value excluding 'A's */
char *next_code (char *code)
/* code is guaranteed to point to a string containing 4 valid hex chars */
{
static char hex_code[5];
unsigned short decimal_value;
decimal_value = (unsigned short)strtol (code, NULL, 16);
do {
decimal_value = ++decimal_value & 0xFFFF; /* FFFF's next is 0000 */
(void)sprintf (hex_code, "%04X", decimal_value);
} while ( strchr (hex_code, 'A') );
return hex_code;
}
/*** TEST ***/
#define TEST
#ifdef TEST
int main (void)
{
char code[5];
(void)printf ("enter hex code : ");
(void)scanf ("%s", code);
(void)printf ("next code is : %s\n", next_code(code) );
return 0;
}
#endif
It works, but since I'm not a C expert, I would appreciate any comments and/or improvements.
Thanks all in advance.
Theophilos.
-----------
There are only 10 kinds of people: Those who understand binary and those who don't.