C#之IP地址和整數(shù)互轉(zhuǎn)的小例子
更新時間: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;
}
}
測試:
復(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)動操作(三層架構(gòu))
這篇文章主要介紹了C# ComboBox的聯(lián)動操作(三層架構(gòu)),根據(jù)下拉框的變化使得下拉框綁定對應(yīng)值,感興趣的小伙伴們可以參考一下2016-05-05
C#調(diào)用攝像頭實現(xiàn)拍照功能的示例代碼
這篇文章主要介紹了C#調(diào)用攝像頭實現(xiàn)拍照功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

