Parsing Command Line Argument Problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pythonista
    New Member
    • Aug 2007
    • 3

    #1

    Parsing Command Line Argument Problem

    Hello Guys,

    Here's my first question here ^_^

    I'm having a hard time in parsing a command line argument in C++. Let's take for example a msisdn.

    # ./foobar 639171234567

    I want to be stored in an unsigned character array like this:

    unsigned char x[0] = 0x63;
    unsigned char x[1] = 0x91;
    unsigned char x[2] = 0x71;
    .
    .
    .

    The input can be obtained from command line or in a file/database. Anyone had a similar problem like this and solved it? Thanks in advance.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by pythonista
    Hello Guys,

    Here's my first question here ^_^

    I'm having a hard time in parsing a command line argument in C++. Let's take for example a msisdn.

    # ./foobar 639171234567

    I want to be stored in an unsigned character array like this:

    unsigned char x[0] = 0x63;
    unsigned char x[1] = 0x91;
    unsigned char x[2] = 0x71;
    .
    .
    .

    The input can be obtained from command line or in a file/database. Anyone had a similar problem like this and solved it? Thanks in advance.
    Hi,
    Welcome to the TSDN.
    Basically after getting the value in argv, use substring to get 2 characters , store it into the array u need till u reach the end of the string.
    By the way have u tried anything on this?
    Raghuram

    Comment

    • pythonista
      New Member
      • Aug 2007
      • 3

      #3
      Originally posted by gpraghuram
      Hi,
      Welcome to the TSDN.
      Basically after getting the value in argv, use substring to get 2 characters , store it into the array u need till u reach the end of the string.
      By the way have u tried anything on this?
      Raghuram
      Hi thanks for the reply,

      I did a similar implementation like you suggested but when I store the value to the unsigned char array it becomes a decimal value. What I wanted to happen is store the value in hex format:

      argv[1] = "6391712345 67";
      // Get the substring(0,2) and store as hex value
      unsigned char x[0] = 0x63;

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by pythinista
        I did a similar implementation like you suggested but when I store the value to the unsigned char array it becomes a decimal value. What I wanted to happen is store the value in hex format:

        argv[1] = "6391712345 67";
        // Get the substring(0,2) and store as hex value
        unsigned char x[0] = 0x63;
        0x63 is just 99. Just write code to convert two characters to decimal and assign that decimal to your char. Be aware that not all vaues will work:

        argv[1] = "AA91712345 67";

        The AA converts to 170, which is too large for a char.

        Comment

        • Jalvath
          New Member
          • Nov 2007
          • 1

          #5
          Hi,

          I too facing the same problem. I want to store the string in char array with hex format as you mentioned below. Pls let me know if you have solved the below problem,.

          Thanks and Regards, Jalvath

          Originally posted by pythonista
          Hello Guys,

          Here's my first question here ^_^

          I'm having a hard time in parsing a command line argument in C++. Let's take for example a msisdn.

          # ./foobar 639171234567

          I want to be stored in an unsigned character array like this:

          unsigned char x[0] = 0x63;
          unsigned char x[1] = 0x91;
          unsigned char x[2] = 0x71;
          .
          .
          .

          The input can be obtained from command line or in a file/database. Anyone had a similar problem like this and solved it? Thanks in advance.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by Jalvath
            Hi,

            I too facing the same problem. I want to store the string in char array with hex format as you mentioned below. Pls let me know if you have solved the below problem,.
            It's considered rude to hijack someone else's thread. Please start your own thread with your own question. Then we can help you.

            Comment

            • RRick
              Recognized Expert Contributor
              • Feb 2007
              • 463

              #7
              You can get the istringstream to do the hex conversion for you, but you need to extract the substrings yourself. I was only able to get the hex values into an int, which means you'll have to transfer the int value to the unsigned char.
              [code=cpp]
              #include <iostream>
              #include <sstream>
              using namespace std;

              int main( void)
              {
              istringstream iss( "79");
              int ic;

              iss >> hex >> ic;

              cout << "Dec Value: " << ic << endl;

              return 0;
              }
              [/code]

              Comment

              Working...