Practical code: IP address and Integer Conversion in C #

Source: Internet
Author: User
Source code
 1   [Structlayout (layoutkind. explicit)]  2   Public   Struct  IP  3   {  4       Public  IP (uint32 value)  5   {  6           This . _ Text1 = 0 ;  7           This . _ Text2 = 0  ;  8           This . _ Text3 = 0  ;  9           This . _ Text4 = 0  ;  10           This . _ Value = Value; 11   }  12       Public  IP (byte text1, byte text2, byte text3, byte text4)  13   {  14           This . _ Value = 0  ;  15           This . _ Text1 = Text1;  16           This . _ Text2 =Text2;  17           This . _ Text3 = Text3;  18           This . _ Text4 = Text4;  19   }  20 [Fieldoffset ( 0  )]  21       Private  Uint32 _ value;  22 [Fieldoffset ( 0  )]  23       Private  Byte _ text1;  24 [Fieldoffset ( 1  )]  25       Private  Byte _ text2;  26 [Fieldoffset ( 2  )]  27       Private Byte _ text3;  28 [Fieldoffset ( 3  )]  29       Private  Byte _ text4;  30   31       Public  Uint32 Value  32   {  33           Get { Return  This  . _ Value ;}  34           Set { This . _ Value = Value ;}  35   }  36       Public  Byte text1  37   {  38           Get { Return  This  . _ Text1 ;}  39           Set { This . _ Text1 = Value ;}  40   }  41       Public  Byte text2  42   {  43           Get { Return  This  . _ Text2 ;}  44           Set { This . _ Text2 = Value ;}  45   }  46       Public  Byte text3  47   {  48           Get { Return  This  . _ Text3 ;}  49           Set { This . _ Text3 = Value ;}  50   }  51       Public  Byte text4  52   {  53           Get { Return  This  . _ Text4 ;}  54           Set { This . _ Text4 = Value ;}  55   }  56   57       Public   Override   String  Tostring ()  58   {  59          Return String. Format ( "  {0}. {1}. {2}. {3}  " , This . _ Text1.tostring (), This  . _ Text2.tostring (),  60               This . _ Text3.tostring (), This  . _ Text4.tostring ());  61   }  62   63      Public   Static   Implicit   Operator  IP (uint32 value)  64   {  65           Return   New  IP (value );  66   }  67       Public   Static   Explicit  Operator  Uint32 (IP address)  68   {  69           Return  IP. _ value;  70   }  71 }
Test
 1   Class  Program  2   {  3       Static  Void Main ( String  [] ARGs)  4   {  5 IP address = New IP ( 192 , 168 , 1 , 1  );  6   Console. writeline (IP );  7 Uint32 value =(Uint32) ip address;  8   Console. writeline (value );  9   Console. writeline (IP. value );  10 IP ip2 = (IP )( 1234567  );  11   Console. writeline (ip2 );  12           13   Console. readkey ();  14   } 15 }

 

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.