C# 将.NET System.Int64转换为IP地址

C# 将.NET System.Int64转换为IP地址,c#,debugging,network-programming,windb,C#,Debugging,Network Programming,Windb,我在Windbg中有以下System.Net.IPAddress对象,并希望将m_地址(4294967295 m_地址)字段转换为相应的虚线四元表示法。我怎样才能做到这一点?我看到m_ToString的值是00000000。这个字符串是如何获得的 Name: System.Net.IPAddress MethodTable: 71c3d860 EEClass: 71a299f0 Size: 40(0x28) bytes File: C:\Wind

我在Windbg中有以下System.Net.IPAddress对象,并希望将m_地址(4294967295 m_地址)字段转换为相应的虚线四元表示法。我怎样才能做到这一点?我看到m_ToString的值是00000000。这个字符串是如何获得的

Name:        System.Net.IPAddress
MethodTable: 71c3d860
EEClass:     71a299f0
Size:        40(0x28) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
727e821c  40018b1        4         System.Int64  1 instance 4294967295 m_Address
727f2248  40018b2       14        System.String  0 instance 00000000 m_ToString
71c42030  40018b6       1c         System.Int32  1 instance        2 m_Family
727e1994  40018b7       18      System.UInt16[]  0 instance 022c20a8 m_Numbers
727e821c  40018b8        c         System.Int64  1 instance 0 m_ScopeId
727f3aa4  40018b9       20         System.Int32  1 instance        0 m_HashCode
71c3d860  40018ad      7ec System.Net.IPAddress  0   static 022c1ff8 Any
71c3d860  40018ae      7f0 System.Net.IPAddress  0   static 022c203c Loopback
71c3d860  40018af      7f4 System.Net.IPAddress  0   static 022c2080 Broadcast
71c3d860  40018b0      7f8 System.Net.IPAddress  0   static 022c2080 None
71c3d860  40018b3      7fc System.Net.IPAddress  0   static 022c20c4 IPv6Any
71c3d860  40018b4      800 System.Net.IPAddress  0   static 022c2108 IPv6Loopback
71c3d860  40018b5      804 System.Net.IPAddress  0   static 022c214c IPv6None
因此,在断点处,m_地址字段似乎没有地址。在执行UdpClient.Receive方法之后。我得到以下信息

0:000> !DumpObj /d 024a20f8
Name:        System.Net.IPAddress
MethodTable: 71c3d860
EEClass:     71a299f0
Size:        40(0x28) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
727e821c  40018b1        4         System.Int64  1 instance 100731402 m_Address
727f2248  40018b2       14        System.String  0 instance 024a2198 m_ToString
71c42030  40018b6       1c         System.Int32  1 instance        2 m_Family
727e1994  40018b7       18      System.UInt16[]  0 instance 024a2120 m_Numbers
727e821c  40018b8        c         System.Int64  1 instance 0 m_ScopeId
727f3aa4  40018b9       20         System.Int32  1 instance        0 m_HashCode
71c3d860  40018ad      7ec System.Net.IPAddress  0   static 022c1ff8 Any
71c3d860  40018ae      7f0 System.Net.IPAddress  0   static 022c203c Loopback
71c3d860  40018af      7f4 System.Net.IPAddress  0   static 022c2080 Broadcast
71c3d860  40018b0      7f8 System.Net.IPAddress  0   static 022c2080 None
71c3d860  40018b3      7fc System.Net.IPAddress  0   static 022c20c4 IPv6Any
71c3d860  40018b4      800 System.Net.IPAddress  0   static 022c2108 IPv6Loopback
71c3d860  40018b5      804 System.Net.IPAddress  0   static 022c214c IPv6None
0:000> !DumpObj /d 024a2198
Name:        System.String
MethodTable: 727f2248
EEClass:     72423444
Size:        32(0x20) bytes
File:        C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll
String:      10.10.1.6
Fields:
      MT    Field   Offset                 Type VT     Attr    Value Name
727f3aa4  40000aa        4         System.Int32  1 instance        9 m_stringLength
727f2c40  40000ab        8          System.Char  1 instance       31 m_firstChar
727f2248  40000ac        c        System.String  0   shared   static Empty

但问题仍然是,将Int64转换为IP地址的数学公式是什么?

IP地址是4个8位八位组

例如,big-endian格式的值0x2414188f将是IP地址“143.24.20.36”

605296783==0x2414188f

0x2414188f==100100000100000101100001111

00100。00010100 . 00011000 . 10001111

因为这是Big-Endian,所以需要颠倒字节的顺序

00100==36

0000100==20

00011000==24

10001111==143

换句话说,取Int64值(也称为long)。将其转换为二进制表示形式(字节[])。读取中的第一个字节并将其转换为整数。这将是ip地址的第四(最后)个八位字节。读取转换为int的第二个字节,这是ip的第三个八位字节。读取第三个字节,这是第二个八位字节。读取最后一个字节,这是第一个八位字节

我现在没有IDE,但这应该可以解决问题

var bytes = BitConverter.GetBytes(value);
var fourthOctet = BitConverter.ToInt32(bytes,0);
var thirdOctet = BitConverter.ToInt32(bytes,1);
var secondOctet = BitConverter.ToInt32(bytes,2);
var firstOctet = BitConverter.ToInt32(bytes,3);

IP地址是4个8位八位字节

例如,big-endian格式的值0x2414188f将是IP地址“143.24.20.36”

605296783==0x2414188f

0x2414188f==100100000100000101100001111

00100。00010100 . 00011000 . 10001111

因为这是Big-Endian,所以需要颠倒字节的顺序

00100==36

0000100==20

00011000==24

10001111==143

换句话说,取Int64值(也称为long)。将其转换为二进制表示形式(字节[])。读取中的第一个字节并将其转换为整数。这将是ip地址的第四(最后)个八位字节。读取转换为int的第二个字节,这是ip的第三个八位字节。读取第三个字节,这是第二个八位字节。读取最后一个字节,这是第一个八位字节

我现在没有IDE,但这应该可以解决问题

var bytes = BitConverter.GetBytes(value);
var fourthOctet = BitConverter.ToInt32(bytes,0);
var thirdOctet = BitConverter.ToInt32(bytes,1);
var secondOctet = BitConverter.ToInt32(bytes,2);
var firstOctet = BitConverter.ToInt32(bytes,3);

Int64
将是8个八位字节,而不是4个…使用
Int64.MaxValue
会发生什么?使用
Int32
不是更好吗?
Int64
将是8个八位字节,而不是4个…使用
Int64.MaxValue
会发生什么?如果使用
Int32
,您不是会更好吗?它不是:var ip=新的IPAddress(value);所以我的目标是确定如何从int64数学上获得IP,而不是通过代码…@d_blk——我很困惑。这个网站是关于代码的。如果你有一个数学问题,你正在寻找。这不是一个必要的数学问题,我只是好奇如何做到这一点。它不是:var ip=new IPAddress(value);所以我的目标是确定如何从int64数学上获得IP,而不是通过代码…@d_blk——我很困惑。这个网站是关于代码的。如果你有一道数学题,那你就是在找。没有必要是一道数学题,我只是好奇这道题是怎么做的。对不起,如果我不理解你的问题,但这就是我从你的帖子上学到的对不起,如果我不理解你的问题,但这就是我从你的帖子上学到的