convert argv[1] to hex

Discussion in 'C Programming' started by cybernerdsx2, Sep 21, 2006.

  1. cybernerdsx2

    cybernerdsx2 Guest

    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.
     
    cybernerdsx2, Sep 21, 2006
    #1
    1. Advertising

  2. cybernerdsx2

    Guest

    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, 2006
    #2
    1. Advertising

  3. cybernerdsx2

    Guest

    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, 2006
    #3
  4. cybernerdsx2

    Ben Pfaff Guest

    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
     
    Ben Pfaff, Sep 21, 2006
    #4
  5. cybernerdsx2

    cybernerdsx2 Guest

    Ben Pfaff wrote:
    > 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?
     
    cybernerdsx2, Sep 21, 2006
    #5
  6. cybernerdsx2

    Ben Pfaff Guest

    "cybernerdsx2" <> 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
     
    Ben Pfaff, Sep 21, 2006
    #6
  7. "cybernerdsx2" <> wrote in message
    news:...
    > 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
     
    Fred Kleinschmidt, Sep 21, 2006
    #7
  8. "cybernerdsx2" <> writes:

    > Ben Pfaff wrote:
    >> 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--
     
    Michal Nazarewicz, Sep 21, 2006
    #8
  9. cybernerdsx2

    cybernerdsx2 Guest

    Fred Kleinschmidt wrote:
    > "cybernerdsx2" <> wrote in message
    > news:...
    > > 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
    }
     
    cybernerdsx2, Sep 21, 2006
    #9
  10. cybernerdsx2

    CBFalconer Guest

    Fred Kleinschmidt wrote:
    > "cybernerdsx2" <> 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
     
    CBFalconer, Sep 21, 2006
    #10
  11. cybernerdsx2 wrote:
    > Ben Pfaff wrote:
    >> 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?


    No, take a look at the strtol documentation, and ask again if
    you need to.
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 22, 2006
    #11
  12. cybernerdsx2

    Guest

    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, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,409
    Neredbojias
    Aug 19, 2005
  2. Bret

    char **argv vs. char* argv[]

    Bret, Aug 31, 2003, in forum: C Programming
    Replies:
    21
    Views:
    4,709
    Richard Heathfield
    Sep 3, 2003
  3. David
    Replies:
    10
    Views:
    6,081
    Richard Heathfield
    Sep 15, 2003
  4. Bengt Richter
    Replies:
    6
    Views:
    536
    Juha Autero
    Aug 19, 2003
  5. chirs
    Replies:
    3
    Views:
    193
    Dr John Stockton
    Dec 1, 2003
Loading...

Share This Page