C#之IP地址和整数互转的小例子
时间:2023-11-21 05:49:19
源码:
[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;
}
}
测试:
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();
}
}
标签:IP地址,整数互转
0
投稿
猜你喜欢
C#权限管理和设计浅谈
2023-10-09 07:14:45
C#中List〈string〉和string[]数组之间的相互转换
2023-07-11 22:33:27
while和for可以相互转换的例子分享
2023-08-23 02:17:46
C#微信公众号开发之用户管理
2023-04-13 02:40:12
GridView基于pulltorefresh实现下拉刷新 上拉加载更多功能(推荐)
2022-08-29 23:51:58
深入理解spring事务
2023-10-13 14:51:36
Java进程间通信之消息队列
2023-05-24 01:44:27
Unity3D快速入门教程
2022-04-03 14:11:14
详解JAVA类加载机制(推荐)
2021-08-10 04:43:10
详解LeakCanary分析内存泄露如何实现
2022-12-03 22:04:40
C#异步方法返回void与Task的区别详解
2021-06-29 10:00:35
C#中Write()和WriteLine()的区别分析
2023-11-04 21:04:23
使用Files.walkFileTree遍历目录文件
2021-09-27 06:12:40
Android 10 启动分析之init语法详解
2022-03-12 05:07:16
Java深入了解数据结构之二叉搜索树增 插 删 创详解
2023-02-14 08:08:00
C语言入门篇--初识指针和指针变量
2022-05-31 06:10:45
java向文件中追加内容与读写文件内容源码实例代码
2021-11-15 11:45:13
dubbo入门指南及demo实例详解
2023-08-24 04:49:07
浅析Android代码质量管理
2021-06-18 23:02:03
Android动态修改ToolBar的Menu菜单示例
2021-10-29 15:36:07