Poperton
Poperton

Reputation: 2127

What does inet_pton do? Shouldn't I get my IP address but in decimal?

std::string ipv4address("192.168.255.18"); 
struct in_addr ipSourceAddress;
inet_pton(AF_INET, ipv4address.c_str(), (void *)&ipSourceAddress);
std::cout << "ipSourceAddress.s_addr: " << ipSourceAddress.s_addr;

I get

ipSourceAddress.s_addr: 318744768

However any online converter gives me 3232300818 which is the value that works on my IP crafter.

Why I get different values?

Upvotes: 1

Views: 251

Answers (1)

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215221

inet_pton (and struct in_addr in general) stores the result in network byte order, i.e. big endian. Print both values in hex and you'll immediately see that. To get it back to a meaningful integer, run it though ntohl.

Upvotes: 3

Related Questions