I read from the man pages that inet_aton() converts the Internet host address from the IPv4 numbers-and-dots notation into binary form (in network byte order) and stores it in the structure. So I tried the below code. Could someone correct me If I am wrong:) Thanks
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc, char **argv)
{
struct in_addr addr;
char a[] = "192.168.24.29";
int ret = 0;
ret = inet_aton(a, &addr);
if (ret != 0)
printf("Valid\n");
else
printf("Invalid\n");
printf("%u\n", addr);
printf("%u\n", addr.s_addr);
}
Output : 488155328 488155328
Expected output: 11000000 10101000 00011000 00011101
inet_aton() converts IPv4 format to binary notation (1's and 0's). Then why it does not provide in binary. I have tried to convert a number to binary format of 8 - bits here, since my previous output was 488155328 I have tried to convert it to binary.
#include<stdio.h>
void bin(unsigned n)
{
unsigned i;
for (i = 1 << 7; i > 0; i = i / 2)
(n & i)? printf("1"): printf("0");
}
int main(void)
{
bin(192);
printf("\n");
bin(168);
printf("\n");
bin(24);
printf("\n");
bin(29);
printf("\n");
printf("My conversion:");
bin(488155328);
}
Output: 11000000 10101000 00011000 00011101 My conversion: 11000000
Expected is : My conversion should be 11000000 10101000 00011000 00011101