Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi,
Was hoping someone could help me out with a slight problem, Im trying to test whether certain strings are valid hex values: here is my code -
#include //
#include /
#include //
#include //void checkHexNumber(char *hex) {
char *tail;
int hexnum;if (strlen(hex) > 2)
return 1;
else
strtol(hex, &tail, 16);
}int main() {
char *hexNumber = "FF";
checkHexNumber(hexNumber);
return 0;
}If tail contains letters then strtol has failed, but how can i tell if it passed?
I tried if(tail == NULL) which I thought would have worked but it didnt, am i doing something noticeably wrong?I would really appreciate a hand with this.
Neil

Neil,
A pointer to the first unrecognized character in the string is stored in tail after the call. So to check the result:
if (strcmp(tail, ""))
// Failed
else
// Suceeded.Good luck.

![]() |
validate xhtml?
|
filter effects in xhtml?
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |