Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > convert argv[1] to hex

Reply
Thread Tools

convert argv[1] to hex

 
 
cybernerdsx2
Guest
Posts: n/a
 
      09-21-2006
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.

 
Reply With Quote
 
 
 
 
bwray314@gmail.com
Guest
Posts: n/a
 
      09-21-2006

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.

 
Reply With Quote
 
 
 
 
bwray314@gmail.com
Guest
Posts: n/a
 
      09-21-2006

(E-Mail Removed) 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.

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
cybernerdsx2
Guest
Posts: n/a
 
      09-21-2006

Ben Pfaff wrote:
> (E-Mail Removed) 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?

 
Reply With Quote
 
Ben Pfaff
Guest
Posts: n/a
 
      09-21-2006
"cybernerdsx2" <(E-Mail Removed)> writes:

> 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
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      09-21-2006

"cybernerdsx2" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> 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


 
Reply With Quote
 
Michal Nazarewicz
Guest
Posts: n/a
 
      09-21-2006
"cybernerdsx2" <(E-Mail Removed)> writes:

> Ben Pfaff wrote:
>> (E-Mail Removed) 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--
 
Reply With Quote
 
cybernerdsx2
Guest
Posts: n/a
 
      09-21-2006

Fred Kleinschmidt wrote:
> "cybernerdsx2" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ps.com...
> > 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
}

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-21-2006
Fred Kleinschmidt wrote:
> "cybernerdsx2" <(E-Mail Removed)> wrote 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"?


Read about the parameters to strtol()

--
Some informative links:
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hex string to hex value tim Python 8 11-23-2005 06:27 PM
Hex Color Codes - Hex 6 <=> Hex 3 lucanos@gmail.com HTML 10 08-18-2005 11:21 PM
hex value in string back to real hex value jack Python 4 09-08-2004 07:11 AM
How to convert an hex string to a Hex number chirs Javascript 3 12-01-2003 10:06 PM
hex(-5) => Futurewarning: ugh, can't we have a better hex than '-'[:n<0]+hex(abs(n)) ?? Bengt Richter Python 6 08-19-2003 07:33 AM



Advertisments