Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:02:24+00:00 2026-06-03T09:02:24+00:00

I have a program and I want to take a command line argument of

  • 0

I have a program and I want to take a command line argument of a MAC address like "./myapp 00:11:22:33:44:55"

Later on the MAC address is used for struct sockaddr_ll.sll_addr in an unsigned char array, each element is one octet of the MAC address. This function (found on a blog) takes the command line argument and seperates it using the colon “:” as the delimiter;

void StringExplode(string str, string separator, vector<string>* results){
    int found;
    found = str.find_first_of(separator);
    while(found != string::npos){
        if(found > 0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length() > 0){
        results->push_back(str);
    }
}

My code taking in the command line argument looks like this;

unsigned char MAC[6];
vector<string> R;
string thisstring = argv[2];
StringExplode(thisstring, ":", &R);
MAC[0] = (unsigned char)atoi(R[0].c_str());
MAC[1] = (unsigned char)atoi(R[1].c_str());
MAC[2] = (unsigned char)atoi(R[2].c_str());
MAC[3] = (unsigned char)atoi(R[3].c_str());
MAC[4] = (unsigned char)atoi(R[4].c_str());
MAC[5] = (unsigned char)atoi(R[5].c_str());
printf("Ethernet %02x:%02x:%02x:%02x:%02x:%02x\n",MAC[0],MAC[1],MAC[2],MAC[3],MAC[4], MAC[5]);

However, this prints out “Ethernet 00:0b:16:21:2c:37”. Is this because atoi() is wrapping around? I can’t seem to pin point the issue here, however, I feel this probably is a bad way of doing things. I’m new to c++ but even to me this feels long winded so I expect someone of the keyboard heros here will be able to explain just how bad it is, and hopefully how I could make this more efficient.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:02:25+00:00Added an answer on June 3, 2026 at 9:02 am

    The problem is that you’re parsing the octets as decimal numbers not hex – 11 = 0xb, 22 = 0x16, etc.

    I’m not sure what the most C++ way of doing this is, but you can e.g. use strtoul with a base argument:

    for(int i = 0; (i < 6) && (i < R.size()); ++i) {
        MAC[i] = (unsigned char)strtoul(R[i].c_str(), NULL, 16);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program where I want to draw a line between two points.
I want to write a program that just like grep command to deal with
I'm trying to implement a program in Xcode that's somewhat like a command line.
I have a program and want to debug it in gdb. Will I see
I have a program where I want to scrap some useful study material for
I have a program that I want to either hide or show certain UIbuttons
I have a small program I want to execute to test something #include <map>
I have program that runs fast enough. I want to see the number of
I want to have a program that reads metadata from an MP3 file. My
I have a console program and I want if the user press ctrl-z the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.