mysql用法:

 select inet_aton('192.168.2.1'); -- 192.168.2.1  3232236033
 
 select inet_ntoa(3232236033);

 

  c#用法:

        public static string IntToIp(long ipInt)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append((ipInt >> 24) & 0xFF).Append(".");
            sb.Append((ipInt >> 16) & 0xFF).Append(".");
            sb.Append((ipInt >> 8) & 0xFF).Append(".");
            sb.Append(ipInt & 0xFF);
            return sb.ToString();
        }

        /// <summary>
        /// long 转ip
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static long IpToInt(string ip)
        {
            string[] items = ip.Split('.');
            //这里|可以换成+ 因为转化二进制 后面的位数都是0 所以能用 |
            return long.Parse(items[0]) << 24
                    | long.Parse(items[1]) << 16
                    | long.Parse(items[2]) << 8
                    | long.Parse(items[3]);
        }

 

  经测试,sql与c#k 方法两者转换结果是一样的

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-28
  • 2022-12-23
  • 2021-10-01
  • 2021-04-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-02
  • 2022-12-23
  • 2021-09-22
  • 2021-08-28
  • 2021-04-17
  • 2022-03-05
相关资源
相似解决方案