Developer's Infos/Network Programming
uint32_t을 IP로 표현하기
삼성동고양이
2011. 9. 15. 15:24
c Syntax
10 1 10 127
00001010 00000001 00001010 01111111
then the 32 bit binary number as one single integer has the decimal value.
1010000000010000101001111111(bin) = 167840383(dec)
you can programmatically convert between these number systems in either direction. consider (and understand) this snippet:
unsigned int ipAddress = 167840383;
unsigned char octet[4] = {0,0,0,0};
for (i=0; i<4; i++){
octet[i] = ( ipAddress >> (i*8) ) & 0xFF;
}
unsigned int ipAddress = 167840383;
unsigned char octet[4] = {0,0,0,0};
for (i=0; i<4; i++)
{
octet[i] = ( ipAddress >> (i*8) ) & 0xFF;
}
now you have the four octets as unsigned chars in the "octet[]" array (char is just an 8-bit integer), and you can print them in the decimal dot format:
c Syntax (Toggle Plain Text)
printf("%d.%d.%d.%d\n",octet[3],octet[2],octet[1],octet[0]);printf("%d.%d.%d.%d\n",octet[3],octet[2],octet[1],octet[0]);