首先要引入动态库
#pragma comment(lib,"ws2_32.lib")
1、string 转 int
int ip_int = inet_addr("127.0.0.1")
std::cout << "ip_int = " << ip_int << std::endl;2、int 转 string
方法一:
in_addr in_addr_;
in_addr_.S_un.S_addr = ip_int; std::string ip_str = inet_ntoa(in_addr_); std::cout << "ip_str = " << ip_str << std::endl;方法二:
std::string ip_str = inet_ntoa(*((in_addr*)&ip_int));
std::cout << "ip_str = " << ip_str << std::endl;