convert argv[1] to hex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cybernerdsx2

    #1

    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.

  • bwray314@gmail.com

    #2
    Re: convert argv[1] to hex


    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.

    Comment

    • bwray314@gmail.com

      #3
      Re: convert argv[1] to hex


      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.

      Comment

      • Ben Pfaff

        #4
        Re: convert argv[1] to hex

        bwray314@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

        Comment

        • cybernerdsx2

          #5
          Re: convert argv[1] to hex


          Ben Pfaff wrote:
          bwray314@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?

          Comment

          • Ben Pfaff

            #6
            Re: convert argv[1] to hex

            "cybernerds x2" <fongjengkum@gm ail.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

            Comment

            • Michal Nazarewicz

              #7
              Re: convert argv[1] to hex

              "cybernerds x2" <fongjengkum@gm ail.comwrites:
              Ben Pfaff wrote:
              >bwray314@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*jab ber.org>--ooO--(_)--Ooo--

              Comment

              • Fred Kleinschmidt

                #8
                Re: convert argv[1] to hex


                "cybernerds x2" <fongjengkum@gm ail.comwrote in message
                news:1158861697 .320051.26600@i 3g2000cwc.googl egroups.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


                Comment

                • cybernerdsx2

                  #9
                  Re: convert argv[1] to hex


                  Fred Kleinschmidt wrote:
                  "cybernerds x2" <fongjengkum@gm ail.comwrote in message
                  news:1158861697 .320051.26600@i 3g2000cwc.googl egroups.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
                  }

                  Comment

                  • CBFalconer

                    #10
                    Re: convert argv[1] to hex

                    Fred Kleinschmidt wrote:
                    "cybernerds x2" <fongjengkum@gm ail.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"?
                    Read about the parameters to strtol()

                    --
                    Some informative links:
                    news:news.annou nce.newusers
                    Latest news coverage, email, free stock quotes, live scores and video are just the beginning. Discover more every day at Yahoo!







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

                    Comment

                    • Nils O. Selåsdal

                      #11
                      Re: convert argv[1] to hex

                      cybernerdsx2 wrote:
                      Ben Pfaff wrote:
                      >bwray314@gmail. com writes:
                      >>
                      >>cybernerdsx 2 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.

                      Comment

                      • mark_bluemel@pobox.com

                        #12
                        Re: convert argv[1] to hex


                        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?

                        Comment

                        Working...