Is there a standard function for converting IP address to number?

Giorgos Keramidas keramida at ceid.upatras.gr
Mon Dec 4 05:12:19 PST 2006


On 2006-12-04 14:45, a at zeos.net wrote:
> Is there a standard function converting four numbers to one 32-bit 
> IP address?
> 
> I mean a function like
> 
> f(i, j, k, l) {
> 	return (((((i << 8) | j) << 8) | k) << 8) | l;
> 	}

That's not even a complete, usable function, but if you are looking for
a 'standard' function as in "part of libc already", the answer is no.

Try something like the

    in_addr_t makeaddr(uint8_t, uint8_t, uint8_t, uint8_t);

function from this sample program:

    http://people.freebsd.org/~keramida/files/addr.tar.gz

which looks like this:

    #include <sys/types.h>
    
    #include <netinet/in.h>
    
    #include "addr.h"
    
    in_addr_t
    makeaddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d)
    {
    
            return (htonl(((in_addr_t)a << 24) + ((in_addr_t)b << 16) +
                ((in_addr_t)c << 8) + (in_addr_t)d));
    }



More information about the freebsd-questions mailing list