-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIpString.cpp
47 lines (34 loc) · 863 Bytes
/
IpString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* \file IpString.cpp
* \brief
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <arpa/inet.h>
#include <cmath>
//-------------------------------------------------------------------------------------------------
int main(int, char **)
{
const std::string ip = "92.60.182.205";
// to int
uint32_t ip_int {};
{
ip_int = ::inet_network( ip.c_str() );
STD_TEST(ip_int == 1547482829);
std::cout << STD_TRACE_VAR(ip_int) << std::endl;
}
// to string
{
struct in_addr ip_addr {};
ip_addr.s_addr = ::htonl(ip_int);
std::string ip_str = inet_ntoa(ip_addr);
STD_TEST(ip_str == ip);
std::cout << STD_TRACE_VAR(ip_str) << std::endl;
}
return 0;
}
//-------------------------------------------------------------------------------------------------
#if OUTPUT
ip_int: 1547482829
ip_str: 92.60.182.205
#endif