开发者

Convert MAC address std::string into uint64_t

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!


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])
        );
}


My solution (requires c++11):

#include <string>
#include <cstdint>
#include <algorithm>
#include <stdlib.h>


uint64_t convert_mac(std::string mac) {
  // Remove colons
  mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());

  // Convert to uint64_t
  return strtoul(mac.c_str(), NULL, 16);
}


Use sscanf:

std::string mac = "00:00:12:24:36:4f";
unsigned u[6];
int c=sscanf(mac.c_str(),"%x:%x:%x:%x:%x:%x",u,u+1,u+2,u+3,u+4,u+5);
if (c!=6) raise_error("input format error");
uint64_t r=0;
for (int i=0;i<6;i++) r=(r<<8)+u[i];
// or:  for (int i=0;i<6;i++) r=(r<<8)+u[5-i];


I can't think of any magic tricks. Here's a random attempt that may or may not be better than what you've done. It's simplish, but I bet there's far faster solutions.

uint64_t mac2int(std::string s) {
    uint64_t r=0;
    std::string::iterator i;
    std::string::iterator end = s.end();

    for(i = s.begin; i != end; ++i) {
        char let = *i;
        if (let >= '0' && let <= '9') { 
            r = r*0xf + (let-'0');
        } else if (let >= 'a' && let <= 'f') { 
            r = r*0xf + (let-'a'+10);
        } else if (let >= 'A' && let <= 'F') { 
            r = r*0xf + (let-'A'+10);
        }
    } 
    return r;
}


This will just shift hex digits through until the string runs out, not caring about delimiters or total length. But it converts the input string to the desired uint64_t format.

#include <string>
#include <stdint.h>

uint64_t cvt(std::string &v)
{
    std::string::iterator i;
    std::string digits = "0123456789abcdefABCDEF";
    uint64_t result = 0;
    size_t pos = 0;

    i = v.begin();

    while (i != v.end())
    {
        // search for character in hex digits set
        pos = digits.find(*i);

        // if found in valid hex digits
        if (pos != std::string::npos)
        {
            // handle upper/lower case hex digit
            if (pos > 0xf)
            {
                pos -= 6;
            }

            // shift a nibble in
            result <<= 4;
            result |= pos;
        }

        ++i;
    }

    return result;
}


Another faster version without calling library functions:

inline unsigned read_hex_byte(char const** beg, char const* end) {
    if(end - *beg < 2)
        throw std::invalid_argument("");
    unsigned hi = (*beg)[0], lo = (*beg)[1];
    *beg += 2;
    hi -= hi >= '0' && hi <= '9' ? '0' :
        hi >= 'a' && hi <= 'f' ? 'a' - 10 :
        hi >= 'A' && hi <= 'F' ? 'A' - 10 :
        throw std::invalid_argument("");
    lo -= lo >= '0' && lo <= '9' ? '0' :
        lo >= 'a' && lo <= 'f' ? 'a' - 10 :
        lo >= 'A' && lo <= 'F' ? 'A' - 10 :
        throw std::invalid_argument("");
    return hi << 4 | lo;
}

uint64_t string_to_mac2(std::string const& s) {
    char const *beg = s.data(), *end = beg + s.size();
    uint64_t r;
    try {
        r = read_hex_byte(&beg, end);
        beg += beg != end && ':' == *beg;
        r = r << 8 | read_hex_byte(&beg, end);
        beg += beg != end && ':' == *beg;
        r = r << 8 | read_hex_byte(&beg, end);
        beg += beg != end && ':' == *beg;
        r = r << 8 | read_hex_byte(&beg, end);
        beg += beg != end && ':' == *beg;
        r = r << 8 | read_hex_byte(&beg, end);
        beg += beg != end && ':' == *beg;
        r = r << 8 | read_hex_byte(&beg, end);
    } catch(std::invalid_argument&) {
        beg = end - 1;
    }
    if(beg != end)
        throw std::runtime_error("invalid mac address format " + s);
    return r;
}


My 2 cents:

uint64_t ParseMac(const std::string& str)
{
  std::istringstream iss(str);
  uint64_t nibble;
  uint64_t result(0);
  iss >> std::hex;
  while(iss >> nibble) {
    result = (result << 8) + nibble;
    iss.get();
  }
  return result;
}


More C++11 way without input data validation:

uint64_t stomac( const std::string &mac )
{
    static const std::regex r{ "([\\da-fA-F]{2})(:|$)" };

    auto it = std::sregex_iterator( mac.begin(), mac.end(), r );
    static const auto end = std::sregex_iterator();

    return std::accumulate( it, end, 0, []( uint64_t i, const std::sregex_iterator::value_type &v ) {
            return ( i << 8 ) + std::stol( v.str(1), nullptr, 16 );
    } );
}

live example


You can also use the ASCII to struct ether_addr conversion routine ether_aton, or its thread-safe version ether_aton_r (GNU extension).

#include <netinet/ether.h>
#include <stdint.h>

#include <string>

#define ETHER_ADDR_ERR UINT64_C(~0)

uint64_t ether_atou64( const std::string& addr_str ) {
    union {
        uint64_t          result;
        struct ether_addr address;
    };
    result = 0;
    struct ether_addr* ptr = ether_aton_r( addr_str.c_str(), &address );
    if( !ptr ) {
        return ETHER_ADDR_ERR;
    }
    return result;
}


Sorry I connot comment yet.

For the answer from @AB71E5, you need to change "strtoul" to "strtoull". Ex : 01:02:03:04:05:06 = 48bits but "unsigned long" = 32bits.

The final result is :

#include <string>
#include <cstdint>
#include <algorithm>
#include <stdlib.h>


uint64_t convert_mac(std::string mac) {
  // Remove colons
  mac.erase(std::remove(mac.begin(), mac.end(), ':'), mac.end());

  // Convert to uint64_t
  return strtoull(mac.c_str(), NULL, 16);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜