"Converting an unsigned long-type IP address to a string-type IP Address"

Source: Internet
Author: User

/* 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.
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.