How to Convert Integer to IP and Vice Versa

ip-addressMany times we need to save and transmit IP address in 32 bit integer format and print it or get it from the user in byte array format, so here is some code snippets to convert between formats.


uint_32 convertIp(uint_8 a,uint_8 b,uint_8 c, uint_8 d)
	uint_32 ip;
	return ip;	

void print_ip(uint_32 ip)
    unsigned char bytes[4];
    bytes[0] = ip & 0xFF;
    bytes[1] = (ip >> 8) & 0xFF;
    bytes[2] = (ip >> 16) & 0xFF;
    bytes[3] = (ip >> 24) & 0xFF;	
    printf("\r\n%d.%d.%d.%d", bytes[3], bytes[2], bytes[1], bytes[0]);        

Leave a Reply

Your email address will not be published. Required fields are marked *