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

  • Home
  • SEARCH
  • 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 6532951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:03:59+00:00 2026-05-25T10:03:59+00:00

I have a hexadecimal MAC address held in a std::string. What would be the

  • 0

I have a hexadecimal MAC address held in a std::string. What would be the best way to turn that MAC address into an integer-type held in a uint64_t?

I’m aware of stringstream, sprintf, atoi, etc. I’ve actually written little conversion functions with the first 2 of those, but they seem more sloppy than I would like.

So, can someone show me a good, clean way to convert

std::string mac = "00:00:12:24:36:4f";

into a uint64_t?

PS: I don’t have boost/TR1 facilities available and can’t install them where the code will actually be used (which is also why I haven’t copy pasted one of my attempts, sorry about that!). So please keep solutions to straight-up C/C++ calls. If you have an interesting solution with a UNIX system call I’d be interested too!

  • 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-05-25T10:04:00+00:00Added an answer on May 25, 2026 at 10:04 am
    uint64_t string_to_mac(std::string const& s) {
        unsigned char a[6];
        int last = -1;
        int rc = sscanf(s.c_str(), "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx%n",
                        a + 0, a + 1, a + 2, a + 3, a + 4, a + 5,
                        &last);
        if(rc != 6 || s.size() != last)
            throw std::runtime_error("invalid mac address format " + s);
        return
            uint64_t(a[0]) << 40 |
            uint64_t(a[1]) << 32 | ( 
                // 32-bit instructions take fewer bytes on x86, so use them as much as possible.
                uint32_t(a[2]) << 24 | 
                uint32_t(a[3]) << 16 |
                uint32_t(a[4]) << 8 |
                uint32_t(a[5])
            );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to convert integer value into hexadecimal. I have done with some logical,
I have a PHP string type variable which may come encoded in Hexadecimal pattern
I have a C++ script which involves string representations of would-be hexadecimal numbers. In
I have a long string (8000 characters) that should contain only hexadecimal and newline
I have a stored procedure that needs to convert hexadecimal numbers to their decimal
The title basically says it all. I have a hexadecimal string representing a private
I have a negative integer (4 bytes) of which I would like to have
Scenario: I have a string of hexadecimal characters which encode 8-bit signed integers. Each
I have noticed that PHP and JavaScript treat octal and hexadecimal numbers with some
I am trying to work out how to split the following hexadecimal string into

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.