C#之IP地址和整數(shù)互轉(zhuǎn)的小例子
更新時(shí)間:2013年03月11日 14:55:29 作者:
C#之IP地址和整數(shù)互轉(zhuǎn)的小例子,需要的朋友可以參考一下
源碼:
復(fù)制代碼 代碼如下:
[StructLayout(LayoutKind.Explicit)]
public struct IP
{
public IP(UInt32 value)
{
this._text1 = 0;
this._text2 = 0;
this._text3 = 0;
this._text4 = 0;
this._value = value;
}
public IP(Byte text1, Byte text2, Byte text3, Byte text4)
{
this._value = 0;
this._text1 = text1;
this._text2 = text2;
this._text3 = text3;
this._text4 = text4;
}
[FieldOffset(0)]
private UInt32 _value;
[FieldOffset(0)]
private Byte _text1;
[FieldOffset(1)]
private Byte _text2;
[FieldOffset(2)]
private Byte _text3;
[FieldOffset(3)]
private Byte _text4;
public UInt32 Value
{
get { return this._value; }
set { this._value = value; }
}
public Byte Text1
{
get { return this._text1; }
set { this._text1 = value; }
}
public Byte Text2
{
get { return this._text2; }
set { this._text2 = value; }
}
public Byte Text3
{
get { return this._text3; }
set { this._text3 = value; }
}
public Byte Text4
{
get { return this._text4; }
set { this._text4 = value; }
}
public override string ToString()
{
return String.Format("{0}.{1}.{2}.{3}", this._text1.ToString(), this._text2.ToString(),
this._text3.ToString(), this._text4.ToString());
}
public static implicit operator IP(UInt32 value)
{
return new IP(value);
}
public static explicit operator UInt32(IP ip)
{
return ip._value;
}
}
測(cè)試:
復(fù)制代碼 代碼如下:
class Program
{
static void Main(string[] args)
{
IP ip = new IP(192,168,1,1);
Console.WriteLine(ip);
UInt32 value = (UInt32)ip;
Console.WriteLine(value);
Console.WriteLine(ip.Value);
IP ip2 = (IP)(1234567);
Console.WriteLine(ip2);
Console.ReadKey();
}
}
相關(guān)文章
C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu))
這篇文章主要介紹了C# ComboBox的聯(lián)動(dòng)操作(三層架構(gòu)),根據(jù)下拉框的變化使得下拉框綁定對(duì)應(yīng)值,感興趣的小伙伴們可以參考一下2016-05-05
C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法
這篇文章主要介紹了C#實(shí)現(xiàn)發(fā)送簡(jiǎn)單HTTP請(qǐng)求的方法,涉及C#操作http的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
C# WinForm國(guó)際化實(shí)現(xiàn)的簡(jiǎn)單方法
這篇文章主要介紹了C# WinForm國(guó)際化實(shí)現(xiàn)的簡(jiǎn)單方法,有需要的朋友可以參考一下2014-01-01
C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼
這篇文章主要介紹了C#調(diào)用攝像頭實(shí)現(xiàn)拍照功能的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
簡(jiǎn)介Winform中創(chuàng)建用戶控件
用戶控件可以讓開發(fā)人員對(duì)VS控件進(jìn)行組裝。下面我們來(lái)創(chuàng)建一個(gè)按鈕的用戶控件我們可以給它添加屬性,并且添加相應(yīng)鼠標(biāo)移入、移出事件。2013-03-03

