IPv4 to decimal values?

Why is the decimal value of IPv4 different from inet_pton and inet_addr (1734763876) than what you get if you use these 2 websites (1684366951)?

struct sockaddr_in sin;
inet_pton(AF_INET, "100.101.102.103", &(sin.sin_addr));
printf("%i\n%i\n", inet_addr("100.101.102.103"), sin.sin_addr);
+3
source share
2 answers

Endianness - they have four bytes in opposite orders:

1734763876 = 0x67 66 65 64
1684366951 = 0x64 65 66 67

The value you need to use for URLs, etc., is the first in the "Network" order, with the most significant byte. Use htonl()(from host to network) to convert the value, i.e.

printf("%i\n%i\n", htonl(inet_addr("100.101.102.103")), htonl(sin.sin_addr));

caf , , , : , - , ..

printf("%i\n%i\n", ntohl(inet_addr("100.101.102.103")), ntohl(sin.sin_addr));
+7

inet_addr .

1684366951 1734763876 - ;-), endianess.

+1

Source: https://habr.com/ru/post/1767151/


All Articles