Int inet_aton (const char * CP)
{
Int DOTS = 0;
Int A [32] = {0 };
Register unsigned long ACC = 0, ADDR = 0;
Do {
Register char cc = * CP;
Switch (cc)
{
Case '0 ':
Case '1 ':
Case '2 ':
Case '3 ':
Case '4 ':
Case '5 ':
Case '6 ':
Case '7 ':
Case '8 ':
Case '9 ':
ACC = ACC * 10 + (CC-'0 ');
Break;
Case '.':
If (++ dots> 3)
{
Return 0;
}
// Fall through
Case '\ 0 ':
If (ACC> 255)
{
Return 0;
}
ADDR = ADDR <8 | ACC; // This sentence is the essence. Each time you move the current value to the left eight places and add the following value ADDR, the final result is an unsigned long integer.
ACC = 0;
Break;
Default:
Return 0;
}
} While (* CP ++ );
// Normalize the address
If (DOTS <3)
{
ADDR <= 8 * (3-dots );
}
Int K = 0;
While (ADDR)
{
A [k ++] = ADDR % 2;
ADDR/= 2;
}
For (int K = 31; k> = 0; k --)
{
Cout <A [k];
If (K % 8 = 0)
{
Cout <"";
}
}
Return 1;
}
Convert an IP address to binary data without calling the library function.