IP Format check, IP-int Conversion

Source: Internet
Author: User
  Public   Static   Bool Checkipaddress ( String IP)
{
String Pattern =   @" (\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) \.) {3} (\ D {1, 2}) | (1 \ D {2}) | (2 [0-4] \ D) | (25 [0-5]) " ;
System. Text. regularexpressions. RegEx =   New System. Text. regularexpressions. RegEx (pattern );
If (RegEx. Match (IP). Success)
{
Return   True ;
}
Else
{
Return   False ;
}
}

Private   Static   Long Iptoint ( String IP)
{
Char [] Separator =   New   Char [] { ' . ' };
If (IP. Split (separator). Length =   3 )
{
IP = IP +   " . 0 " ;
}
String [] Strarray = IP. Split (separator );
Long Num2 = (( Long . Parse (strarray [ 0 ]) *   0x100l ) *   0x100l ) *   0x100l ;
Long Num3 = ( Long . Parse (strarray [ 1 ]) *   0x100l ) *   0x100l ;
Long Num4 =   Long . Parse (strarray [ 2 ]) *   0x100l ;
Long Num5 =   Long . Parse (strarray [ 3 ]);
Return (Num2 + Num3) + Num4) + Num5 );
}

Private   Static   String Inttoip ( Long Ip_int)
{
Long Num = ( Long ) (Ip_int &   0xff000000l ) >   0x18 );
If (Num <   0l )
{
Num + =   0x100l ;
}
Long Num2 = (Ip_int &   0xff0000l ) >   0x10 ;
If (Num2 <   0l )
{
Num2 + =   0x100l ;
}
Long Num3 = (Ip_int &   0xff00l ) >   8 ;
If (Num3 <   0l )
{
Num3 + =   0x100l ;
}
Long Num4 = Ip_int &   0 xffl ;
If (Num4 <   0l )
{
Num4 + =   0x100l ;
}
Return (Num. tostring () +   " . "   + Num2.tostring () +   " . "   + Num3.tostring () +   " . "   + Num4.tostring ());
}

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.