"cybernerdsx2" <fo*********@gmail.comwrites:
Ben Pfaff wrote:
>bw******@gmail.com writes:
cybernerdsx2 wrote:
So how can I convert the argv[1] to unsgined char so that I can compare
it like this:
An obvious solution would be to write a function that scans through the
characters in argv[1] (right to left) and adds up successive powers of
16 to result in your unsigned integer.
Why would you want to do this right-to-left? Left-to-right
conversion is at least as easy. And why would you want to
reinvent strtol() unnecessarily?
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Ok, so all I need to do is use strtol( argv[1] ) and assign it to my
unsigned char variable, ucCMD and it should work?
Sort of but you should also check for errors, like so:
#v+
#include <stdio.h>
#include <stdlib.h>
#include <limit.h>
/* various stuff */
int main(int argc, char **argv) {
unsigned char cmd;
/* probably some more variables */
if (argc==1) {
fprintf(stderr, "%s: not enough parameters\n", *argv);
return 1;
} else {
char *end;
long val = strtol(argv[1], &end, 16);
if (*end || val<0 || val>UCHAR_MAX) {
fprintf(stderr, "%s: invalid number: %s\n", *argv, argv[1]);
return 1;
}
cmd = val;
}
/* some more stuff */
}
#v-
BTW. Does (val<0 || val>UCHAR_MAX) equal to (!!(val & ~UCHAR_MAX))?
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--