By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,384 Members | 671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,384 IT Pros & Developers. It's quick & easy.

Parsing Command Line Argument Problem

P: 3
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.
Aug 10 '07 #1
Share this Question
Share on Google+
6 Replies


gpraghuram
Expert 100+
P: 1,275
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
Aug 10 '07 #2

P: 3
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] = "639171234567";
// Get the substring(0,2) and store as hex value
unsigned char x[0] = 0x63;
Aug 10 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
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] = "639171234567";
// 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] = "AA9171234567";

The AA converts to 170, which is too large for a char.
Aug 10 '07 #4

P: 1
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

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.
Nov 7 '07 #5

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Nov 7 '07 #6

RRick
Expert 100+
P: 463
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. int main( void)
  6. {
  7.     istringstream iss( "79");
  8.     int ic;
  9.  
  10.     iss >> hex >> ic;
  11.  
  12.     cout << "Dec Value: " << ic << endl;
  13.  
  14.     return 0;
  15. }
  16.  
Nov 7 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.