471,873 Members | 2,094 Online

# convert argv[1] to hex

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.

Sep 21 '06 #1
11 7863

cybernerdsx2 wrote:
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.

Sep 21 '06 #2

bwray...@gmail.com wrote:
cybernerdsx2 wrote:
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.

Sep 21 '06 #3
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?
--
--Richard Heathfield
Sep 21 '06 #4

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

Sep 21 '06 #5
"cybernerdsx2" <fo*********@gmail.comwrites:
Ben Pfaff wrote:
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.
--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Sep 21 '06 #6
"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?
--
--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--
Sep 21 '06 #7

"cybernerdsx2" <fo*********@gmail.comwrote in message
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"?
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Sep 21 '06 #8

Fred Kleinschmidt wrote:
"cybernerdsx2" <fo*********@gmail.comwrote in message
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"?
--
Fred L. Kleinschmidt
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
}

Sep 21 '06 #9
Fred Kleinschmidt wrote:
"cybernerdsx2" <fo*********@gmail.comwrote in message
.... snip ...
>>
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"?

--
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #10
cybernerdsx2 wrote:
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?
--
--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?
No, take a look at the strtol documentation, and ask again if
you need to.
Sep 22 '06 #11

cybernerdsx2 wrote:
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?

Sep 22 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion.