C # get native IP address and convert string

Source: Internet
Author: User

        /// <summary>        ///IP Address Conversions/// </summary>        /// <param name= "ipaddr" >IP address for integer type</param>        /// <returns>the IP address of the string</returns>        Private stringUINTIPTOSTRINGIP (UINTipaddr) {            stringHexstr = ipaddr. ToString ("X8"); intIP1 = Convert.ToInt32 (hexstr.substring (0,2), -); intIP2 = Convert.ToInt32 (hexstr.substring (2,2), -); intIP3 = Convert.ToInt32 (hexstr.substring (4,2), -); intIP4 = Convert.ToInt32 (hexstr.substring (6,2), -); returnIP4 +"."+ IP3 +"."+ IP2 +"."+ip1; }        /// <summary>        ///IP Address Conversions/// </summary>        /// <param name= "ipaddr" >the IP address of the string</param>        /// <returns>IP address for integer type</returns>        Private UINTStringiptouintip (stringipaddr) {            string[] ips = ipaddr. Split ('.'); returnConvert.touint32 (ips[3]) * the* the* the+ Convert.touint32 (ips[2]) * the* the+ Convert.touint32 (ips[1]) * the+ Convert.touint32 (ips[0]); }        /// <summary>        ///get the native IP address/// </summary>        /// <returns></returns>        Private stringgetlocalipaddress () {stringResultip =string.            Empty; System.net.ipaddress[] IPs=System.Net.Dns.GetHostEntry (System.Net.Dns.GetHostName ()).            AddressList; foreach(System.Net.IPAddress IPinchIPs) {                if(Iscorrentip (IP. ToString ())) {Resultip=IP.                    ToString ();  Break; }            }            returnResultip; }        /// <summary>        ///Verify that the IP address is valid/// </summary>        /// <param name= "IP" ></param>        /// <returns></returns>        Private BOOLIscorrentip (stringIP) {            stringPATTRN =@"(\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5]) \. (\d{1,2}|1\d\d|2[0-4]\d|25[0-5])"; if(System.Text.RegularExpressions.Regex.IsMatch (IP, pattrn)) {return true; }            Else            {                return false; }        }

C # get native IP address and convert string

Related Article

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.