The language does give it tyou in sscanf, but sscanf is a pretty big
function and in some environments, like small embedded ones, you don't
get the luxury of using a big block of code to do a small thing.
unsigned hex_to_unsigned(char *p)
{
unsigned val = 0;
while (*p != '\0') {
char c = *p++;
if ('a' <= c && c <= 'f')
val = (val << 4) + (c - 'a' + 10);
else if ('A' <= c && c <= 'F')
val = (val << 4) + (c - 'A' + 10);
else if ('0' <= c && c <= '9')
val = (val << 4) + (c - '0');
else break; // quit early on non-hex char
}
return val;
}
On Sep 1, 12:34 pm, rShetty <[email protected]> wrote:
> Given a Hexadecimal value as a string, give a C Code to convert it
> into decimal value?
> If 0xff then output should be 255.
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.