#include <iostream> #include <sstream> #include <map> #include <list> #include <vector> #include <string> #include <stdio.h> #include <string.h> #include <stdlib.h> using namespace std; /* 处理IP字符串 */ uint32_t aton(const char str[]) { uint32_t rs = 0; char *p = (char *)str; p = strtok(p, "."); while(p != NULL) { rs = (rs << 8) | atoi(p); p = strtok(NULL, "."); } return rs; } int main() { std::string ip = "64.91.107.58"; uint32_t x = aton(ip.data()); cout << x << endl; return 0; }
原理如下:
最终的结果 = 64 << 24 | 91 << 16 | 107 << 8 | 58
简单明了。