1

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

Paul
  • 331
  • 2
  • 7
  • 1
    If you use %u then your program will print a four byte integer as an unsigned base 10 number. If you want it to print binary then you have to write code that does that. – Jerry Jeremiah May 28 '20 at 08:59
  • This may be useful: https://stackoverflow.com/questions/111928/is-there-a-printf-converter-to-print-in-binary-format – Jerry Jeremiah May 28 '20 at 09:01
  • 1
    This should answer your question [How to convert standard IP address format string to hex and long?](https://stackoverflow.com/questions/491060/how-to-convert-standard-ip-address-format-string-to-hex-and-long) – Roberto Caboni May 28 '20 at 10:03
  • Maybe, respect the topic of the question I linked, you need just a further clarification: you seem to print the converted address expecting a _binary representation_... but consider that the same data can be represented in different ways, and format `%u` won't print binary numbers in any case. The closest thing to binary representation you can do is printing the number in hexadecimal format, using specifier `%08X`. – Roberto Caboni May 28 '20 at 10:08
  • 1
    @ Roberto Caboni Thanks a a lot. Now I get it. – Paul May 28 '20 at 10:11
  • 1
    The decimal number `‭488155328‬` corresponds to hex number `0x‭1D18A8C0‬`. Hex numbers have a fantastic property: two digits represent one byte. So, byte-by-byte you have `0x‭1D 18 A8 C0‬` that is `29 18 168 192` in decimal. It seems you IP "reversed"... and it is. Because printing an integer you are trying to interpret it in machine order (little endian) a number that was stored in network order (big endian). – Roberto Caboni May 28 '20 at 10:13

0 Answers0