convert argv[1] to hex

C

cybernerdsx2

Hi,

I have a program that check for the hex value that passes in from the
command line.

Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.

So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

if ( ucCMD == SOME_CMD )
{
//do something
}

ucCMD is declared as unsigned char.
 
B

bwray314

cybernerdsx2 said:
Hi,

I have a program that check for the hex value that passes in from the
command line.

Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.

So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

if ( ucCMD == SOME_CMD )
{
//do something
}

ucCMD is declared as unsigned char.

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.
 
B

bwray314

cybernerdsx2 said:
Hi,

I have a program that check for the hex value that passes in from the
command line.

Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.

So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

if ( ucCMD == SOME_CMD )
{
//do something
}

ucCMD is declared as unsigned char.

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.

PS: You could also look up a function called sscanf() in your favorite
standard C library book.
 
B

Ben Pfaff

cybernerdsx2 said:
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?
 
C

cybernerdsx2

Ben said:
cybernerdsx2 said:
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?

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?
 
B

Ben Pfaff

cybernerdsx2 said:
Ben said:
cybernerdsx2 wrote:
So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

And why would you want to reinvent strtol() unnecessarily?

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?

You should look up the usage of strtol before you try to use it.
It takes three arguments.
 
F

Fred Kleinschmidt

cybernerdsx2 said:
Hi,

I have a program that check for the hex value that passes in from the
command line.

Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.

So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

if ( ucCMD == SOME_CMD )
{
//do something
}

ucCMD is declared as unsigned char.

Why unsigned char?
What happens if the user inputs "30" instead of "0x3c"?
 
M

Michal Nazarewicz

cybernerdsx2 said:
Ben said:
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?

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))?
 
C

cybernerdsx2

Fred said:
cybernerdsx2 said:
Hi,

I have a program that check for the hex value that passes in from the
command line.

Let say user type in c:\demo.exe 3c
and in the program, I #define SOME_CMD 0x3c.

So how can I convert the argv[1] to unsgined char so that I can compare
it like this:

if ( ucCMD == SOME_CMD )
{
//do something
}

ucCMD is declared as unsigned char.

Why unsigned char?
What happens if the user inputs "30" instead of "0x3c"?

Well, i tried inputting "30" and it didn't give me error.

Ok, now I have the code working, but next thing I need to do is that
argv[1] will have 3 bytes (eg. 3c0240) input from user, how can I put
each byte into a structure that looks something like below?

typedef struct t_cmd
{
unsigned char cmd1;
unsigned char cmd2;
unsigned char cmd3;
} T_CMD;

So that I can access each of them by simply doing it like this:

T_CMD t_cmdvar;

if ( t_cmdvar.cmd1 == CMD1 )
{
if ( t_cmdvar.cmd2 == CMD_A )
{
// do something
}
} else if ( t_cmdvar.cmd1 == CMD2 )
{
// do something
}
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

cybernerdsx2 said:
Ben said:
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?

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?

No, take a look at the strtol documentation, and ask again if
you need to.
 
M

mark_bluemel

cybernerdsx2 said:
Well, i tried inputting "30" and it didn't give me error.

Conclusive proof of correctness, then.
Ok, now I have the code working, but next thing I need to do is that
argv[1] will have 3 bytes (eg. 3c0240) input from user, how can I put
each byte into a structure that looks something like below?

typedef struct t_cmd
{
unsigned char cmd1;
unsigned char cmd2;
unsigned char cmd3;
} T_CMD;

So argv[1] will be a string consisting of "3c0240", for example? Which
is an array of 7 characters '3','c','0,'2,'4','0','\0' - correct?

Why don't you now think about how you could take two characters at a
time out of that string, convert them to bytes and store them in your
structure, and come back to us when/if you have further questions?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top