#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iostream>
uint32_t addr_ston(const char* host) {
uint32_t iaddr = inet_addr(host);
return htonl(iaddr);
}
char* addr_ntos(const uint32_t host) {
uint32_t iaddr = htonl(host);
struct in_addr inaddr{iaddr};
return inet_ntoa(inaddr);
}
int main(int argc, char** argv) {
uint32_t host = addr_ston("192.168.0.1");
// c0a80001
std::cout << std::hex << host << std::dec << std::endl;
// 192.168.0.1
std::cout << addr_ntos(host) << std::endl;
}
IP地址uint32与string互换
于 2022-04-12 14:09:37 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)