Valid signed Hex to long function
Posted
by Ben
on Stack Overflow
See other posts from Stack Overflow
or by Ben
Published on 2010-05-06T14:34:45Z
Indexed on
2010/05/06
14:38 UTC
Read the original article
Hit count: 269
I am trying to convert a 24bit Hexadecimal string (6 characters) signed in two's complement to a long int in C.
This is the function I have come up with:
long int hex2li (char string[])
{
char *pEnd;
long int result = strtol (string, &pEnd, 16);
if (strcmp (pEnd, "") == 0)
{
if (toupper (string[0]) == 'F')
{
return result - 16777216;
}
else
{
return result;
}
}
return LONG_MIN;
}
Is it valid? Is there a better way of doing this?
© Stack Overflow or respective owner