/* Convert an unsigned long-type IP address to a string-type IP Address */
# Define iptosbuffers 12
Char * iptos (u_long in)
{
Static char output [iptosbuffers] [3*4 + 3 + 1]; // The IP string format contains four three-digit numbers plus three vertices and an ending character.
Static short which;
U_char * P;
P = (u_char *) & in;
Which = (which + 1 = iptosbuffers? 0: Which + 1 );
Sprintf (output [which], "% d. % d. % d. % d ", P [0], p [1], p [2], p [3]);
Return output [which];
}
Explanation: http://topic.csdn.net/t/20051007/20/4310836.html
This is a conversion function for a special application.
# Define iptosbuffers 12 // defines the buffer size as 12, which is known from the following function
// This function can retain 11 conversion results
Char * iptos (u_long in)
{
Static char output [iptosbuffers] [3*4 + 3 + 1]; // output a string of 3 to 4 segments, which can hold 12 computation results, but the first one is not used
Static short which; // count
U_char * P;
P = (u_char *) & in; // pointer to the character type of the long integer IP Address
Which = (which + 1 = iptosbuffers? 0: Which + 1); // every time this function is called, add 1 to the counter
Sprintf (output [which], "% d. % d. % d. % d ", P [0], p [1], p [2], p [3]); // converts it to a string of 3 to 4 segments, such as 192.168.0.1, and save it to
// Buffer output
Return output [which]; // return the conversion result.
}
Struct sockaddr_in *;
......
Iptos (a-> sin_addr.s_addr );
-------
This function can retain 11 conversion results, but the results are inside the sub-function and do not know what the programmer is doing. If you do not need these results, you can simply adapt them:
Char * iptos (u_long in)
{
Static char output [3*4 + 3 + 1]; // output a string of 3 to 4 segments
U_char * P;
P = (u_char *) & in; // pointer to the character type of the long integer IP Address
Sprintf (output, "% d. % d", P [0], p [1], p [2], p [3]);
Return output; // return the conversion result.
}