C#之IP地址和整数互转的小例子
发布时间:2023-11-21 05:49:19
标签:IP地址,整数互转
源码:
[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();
}
}


猜你喜欢
- 在application.xml加上以下配置mybatis-plus.configuration.map-underscore-to-cam
- 最近项目里有用到ViewPager来做广告运营位展示,看到现在很多APP的广告运营位都是无限循环的,所以就研究了一下这个功能的实现。先看看效
- 本文实例为大家分享了C#基于Sockets类实现TCP通讯的具体代码,供大家参考,具体内容如下最终效果TCPClientusing Syst
- 关于Function.identity()的使用简单介绍话不多说,直接上JDK源码:static Function identity() {
- 前言Spring是什么?它是一个应用程序框架,为应用程序的开发提供强大的支持,例如对事务处理和持久化的支持等;它也是一个bean容器,管理b
- controller传boolean形式值@GetMapping("/check-cart")public List&l
- 在 Eclipse 里新建好工程后,默认会有一个assets目录,在 Eclipse 中直接将准备好的 SQLite 数据库复制到该目录中,
- Monkey 是Android SDK提供的一个命令行工具, 可以简单,方便地运行在任何版本的Android模拟器和实体设备上。 Monke
- 目录引言什么是Span关于String的一段性能提升测试代码最终性能对比写在最后引言C# 是一门现代化的编程语言,与Java十分的相似。熟练
- Android Studio软件下载地址如下:下载:http://www.android-studio.org/index.php/down
- 基于SpringCloud微服务平台,进行服务实例监控及健康检查,注册中心为eureka,SpringBoot提供了很好的组件SpringB
- 问题springboot 集成springcloud时常常由于版本问题而报错,如下:com.sun.jersey.api.client.Cl
- 今天给大家带来一篇简单易懂的微技巧文章,并没有什么高深的技术点,但重点是在细节,相信可以给不少朋友带来帮助。Dialog和Toast所有人肯
- Spring Aop的原理Spring的AOP就是通过 * 实现的。当为某个Bean或者某些Bean配置切面时,Spring会为其创建代理
- C# 获取硬件参数的实现方法示例代码:private static string GetIdentifier(string wmiClass
- 相关方法:wait():一旦执行此方法,当前线程就进入阻塞状态,并释放同步监视器。notify():一旦执行此方法,就会唤醒被wait的一个
- 在并发多线程的情况下,为了保证数据安全性,一般我们会对数据进行加锁,通常使用Synchronized或者ReentrantLock同步锁。S
- 前言最近在改进项目的并发功能,但开发起来磕磕碰碰的。看了好多资料,总算加深了认识。于是打算配合查看源代码,总结并发编程的原理。准备从用得最多
- 1. openFeign实现基于spring-boot-starter-parent 2.6.8,spring-cloud-dependen
- 前言在现实项目中,数据量一般都不小,如果一次性全部请求出来,肯定是影响性能,而且大量数据展示到页面上观感也不好。这时我们就需要用到分页,给定