Computing.Net > Forums > Programming > C - strtol

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

C - strtol

Reply to Message Icon

Name: Neil
Date: March 11, 2003 at 15:05:10 Pacific
OS: windows xp
CPU/Ram: 256
Comment:

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



Sponsored Link
Ads by Google

Response Number 1
Name: triet
Date: March 11, 2003 at 18:43:03 Pacific
Reply:

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.


0
Reply to Message Icon

Related Posts

See More


validate xhtml? filter effects in xhtml?



Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C - strtol

Programming in C www.computing.net/answers/programming/programming-in-c/9073.html

C++ HELP - symbol reference errors www.computing.net/answers/programming/c-help-symbol-reference-errors/17051.html

C++ prgramming help www.computing.net/answers/programming/c-prgramming-help/18183.html