<i id='cRXdl'><tr id='cRXdl'><dt id='cRXdl'><q id='cRXdl'><span id='cRXdl'><b id='cRXdl'><form id='cRXdl'><ins id='cRXdl'></ins><ul id='cRXdl'></ul><sub id='cRXdl'></sub></form><legend id='cRXdl'></legend><bdo id='cRXdl'><pre id='cRXdl'><center id='cRXdl'></center></pre></bdo></b><th id='cRXdl'></th></span></q></dt></tr></i><div id='cRXdl'><tfoot id='cRXdl'></tfoot><dl id='cRXdl'><fieldset id='cRXdl'></fieldset></dl></div>
  • <tfoot id='cRXdl'></tfoot>
          <bdo id='cRXdl'></bdo><ul id='cRXdl'></ul>
      1. <small id='cRXdl'></small><noframes id='cRXdl'>

        <legend id='cRXdl'><style id='cRXdl'><dir id='cRXdl'><q id='cRXdl'></q></dir></style></legend>

        如何在 C# 中将 IPv4 地址转换为整数?

        How to convert an IPv4 address into a integer in C#?(如何在 C# 中将 IPv4 地址转换为整数?)
          <i id='xQ9hX'><tr id='xQ9hX'><dt id='xQ9hX'><q id='xQ9hX'><span id='xQ9hX'><b id='xQ9hX'><form id='xQ9hX'><ins id='xQ9hX'></ins><ul id='xQ9hX'></ul><sub id='xQ9hX'></sub></form><legend id='xQ9hX'></legend><bdo id='xQ9hX'><pre id='xQ9hX'><center id='xQ9hX'></center></pre></bdo></b><th id='xQ9hX'></th></span></q></dt></tr></i><div id='xQ9hX'><tfoot id='xQ9hX'></tfoot><dl id='xQ9hX'><fieldset id='xQ9hX'></fieldset></dl></div>
            <legend id='xQ9hX'><style id='xQ9hX'><dir id='xQ9hX'><q id='xQ9hX'></q></dir></style></legend>
          • <tfoot id='xQ9hX'></tfoot>
              <tbody id='xQ9hX'></tbody>

                <bdo id='xQ9hX'></bdo><ul id='xQ9hX'></ul>

                1. <small id='xQ9hX'></small><noframes id='xQ9hX'>

                  本文介绍了如何在 C# 中将 IPv4 地址转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在寻找将标准 IPv4 地址转换为整数的函数.可用于执行相反操作的功能的奖励积分.

                  I'm looking for a function that will convert a standard IPv4 address into an Integer. Bonus points available for a function that will do the opposite.

                  解决方案应该在 C# 中.

                  Solution should be in C#.

                  推荐答案

                  32 位无符号整数 IPv4 地址.同时,IPAddress.Address 属性虽然已弃用,但它是一个 Int64,它返回 IPv4 地址的无符号 32 位值(问题是,它是按网络字节顺序排列的,因此您需要交换它周围).

                  32-bit unsigned integers are IPv4 addresses. Meanwhile, the IPAddress.Address property, while deprecated, is an Int64 that returns the unsigned 32-bit value of the IPv4 address (the catch is, it's in network byte order, so you need to swap it around).

                  例如,我的本地 google.com 位于 64.233.187.99.这相当于:

                  For example, my local google.com is at 64.233.187.99. That's equivalent to:

                  64*2^24 + 233*2^16 + 187*2^8 + 99
                  = 1089059683
                  

                  确实,http://1089059683/ 按预期工作(至少在 Windows 中,用 IE、Firefox 和 Chrome 测试;但在 iPhone 上不工作).

                  And indeed, http://1089059683/ works as expected (at least in Windows, tested with IE, Firefox and Chrome; doesn't work on iPhone though).

                  这是一个显示两种转换的测试程序,包括网络/主机字节交换:

                  Here's a test program to show both conversions, including the network/host byte swapping:

                  using System;
                  using System.Net;
                  
                  class App
                  {
                      static long ToInt(string addr)
                      {
                          // careful of sign extension: convert to uint first;
                          // unsigned NetworkToHostOrder ought to be provided.
                          return (long) (uint) IPAddress.NetworkToHostOrder(
                               (int) IPAddress.Parse(addr).Address);
                      }
                  
                      static string ToAddr(long address)
                      {
                          return IPAddress.Parse(address.ToString()).ToString();
                          // This also works:
                          // return new IPAddress((uint) IPAddress.HostToNetworkOrder(
                          //    (int) address)).ToString();
                      }
                  
                      static void Main()
                      {
                          Console.WriteLine(ToInt("64.233.187.99"));
                          Console.WriteLine(ToAddr(1089059683));
                      }
                  }
                  

                  这篇关于如何在 C# 中将 IPv4 地址转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                  1. <small id='z7Uk9'></small><noframes id='z7Uk9'>

                    <i id='z7Uk9'><tr id='z7Uk9'><dt id='z7Uk9'><q id='z7Uk9'><span id='z7Uk9'><b id='z7Uk9'><form id='z7Uk9'><ins id='z7Uk9'></ins><ul id='z7Uk9'></ul><sub id='z7Uk9'></sub></form><legend id='z7Uk9'></legend><bdo id='z7Uk9'><pre id='z7Uk9'><center id='z7Uk9'></center></pre></bdo></b><th id='z7Uk9'></th></span></q></dt></tr></i><div id='z7Uk9'><tfoot id='z7Uk9'></tfoot><dl id='z7Uk9'><fieldset id='z7Uk9'></fieldset></dl></div>
                        <bdo id='z7Uk9'></bdo><ul id='z7Uk9'></ul>
                        <legend id='z7Uk9'><style id='z7Uk9'><dir id='z7Uk9'><q id='z7Uk9'></q></dir></style></legend>

                          <tfoot id='z7Uk9'></tfoot>

                              <tbody id='z7Uk9'></tbody>