zoukankan      html  css  js  c++  java
  • IP与bigint互转



    /****** 对象: UserDefinedFunction [dbo].[UF_CovertIPToInt] 脚本日期: 08/06/2012 16:55:22 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO



    CREATE FUNCTION [dbo].[UF_CovertIPToInt]
    (
    @ip varchar(20)
    )
    RETURNS bigint
    AS
    BEGIN
    declare @pos int
    declare @sub1 varchar(10)
    declare @sub2 varchar(10)
    declare @sub3 varchar(10)
    declare @sub4 varchar(10)

    set @pos = charindex('.',@ip)

    if(@pos<1) return 0

    set @sub1 = substring(@ip,0,@pos)

    --select @sub1

    set @ip = substring(@ip, @pos+1 ,len(@ip) - @pos + 1)

    --select @ip

    set @pos = charindex('.',@ip)

    if(@pos<1) return 0

    set @sub2 = substring(@ip,0,@pos)

    --select @sub2

    set @ip = substring(@ip, @pos+1 ,len(@ip) - @pos + 1)

    --select @ip

    set @pos = charindex('.',@ip)

    if(@pos<1) return 0

    set @sub3 = substring(@ip,0,@pos)

    --select @sub3

    set @ip = substring(@ip, @pos+1 ,len(@ip) - @pos + 1)

    if(len(@ip)<1) return 0

    select @sub4 = @ip

    --select @sub4

    return cast(@sub1 as bigint ) * 16777216 + cast(@sub2 as bigint ) * 65536 + cast(@sub3 as bigint )*256 + cast(@sub4 as bigint )

    END ;


    USE Lambor_Game
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO


    CREATE FUNCTION [dbo].[UF_ConvertIPToStr]
    (
    @ip bigint
    )
    RETURNS varchar(20)
    AS
    BEGIN
    RETURN cast((@ip/16777216)% 256 as varchar(4)) + '.' + cast( (@ip/65536)% 256 as varchar(4)) +'.'+ cast( (@ip/256)%256 as varchar(4))+ '.'+ cast( (@ip%256)as varchar(4))

    END
    ;

    --把IP转为bigint
    select dbo.UF_CovertIPToInt('192.168.1.100') as ip ;
    --把bigint转换为IP
    select dbo.UF_ConvertIPToStr (3232235876) as ip

  • 相关阅读:
    搭建负载均衡的环境(利用虚拟机上的四台centos)
    java的IO,AIO简单对比
    【每日分享】关于漏测
    安装xampp后,遇到的各种问题
    端口占用问题——netstat命令
    随笔
    AJAX 状态值(readyState)与状态码(status)详解
    CSS 实践:实现下拉菜单的方法
    css3动画总结
    判断手机运营商
  • 原文地址:https://www.cnblogs.com/yxcn/p/12889084.html
Copyright © 2011-2022 走看看