当前位置:七道奇文章资讯数据防范MSSQL防范
日期:2011-05-02 15:21:00  来源:本站整理

若何将作为字符串保存的IP地址变成二进制数值?[MSSQL防范]

赞助商链接



  本文“若何将作为字符串保存的IP地址变成二进制数值?[MSSQL防范]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
关于sql server 2000,你可以利用下列函数:

CREATE FUNCTION dbo.IPAddrStr2Bin(@strIP varchar(15), @Validate bit = 1)
RETURNS binary(4)
AS
BEGIN
IF @Validate = 1
BEGIN
-- only digits and dots
IF @strIP LIKE '%[^.0-9]%' RETURN (NULL)
-- number of dots must be 3
IF LEN(@strIP) - LEN(REPLACE(@strIP, '.', '')) != 3 RETURN (NULL)
-- all octets must be specified
IF @strIP NOT LIKE '%_%.%_%.%_%.%_%' RETURN (NULL)
END
DECLARE @oct1 binary(1),
@oct2 binary(1),
@oct3 binary(1),
@oct4 binary(1)
SET @oct1 = CAST(CAST(LEFT(@strIP, CHARINDEX('.', @strIP) - 1) AS int) AS
binary(1))
SET @oct2 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP) + 1,
CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1) - CHARINDEX('.', @strIP)
- 1) AS int) AS binary(1))
SET @oct3 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP,
CHARINDEX('.', @strIP) + 1) + 1, (LEN(@strIP) - CHARINDEX('.',
REVERSE(@strIP)) + 1) - (CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1))
- 1) AS int) AS binary(1))
SET @oct4 = CAST(CAST(RIGHT(@strIP, LEN(@strIP) - (LEN(@strIP) -
CHARINDEX('.', REVERSE(@strIP)) + 1)) AS int) AS binary(1))
IF @Validate = 1
BEGIN
IF NOT(
(@oct1 BETWEEN 0x01 AND 0xFF) AND
(@oct2 BETWEEN 0x00 AND 0xFF) AND
(@oct3 BETWEEN 0x00 AND 0xFF) AND
(@oct4 BETWEEN 0x00 AND 0xFF)
) RETURN(NULL)
END
RETURN (@oct1 + @oct2 + @oct3 + @oct4)
END
GO

利用例子:
SELECT dbo.IPAddrStr2Bin('172.29.23.2', 0)

关于 SQL Server较早的版本,你可以抽出代码,然后直接利用,大概成立以下所示的存储历程:

CREATE PROCEDURE dbo.spIPAddrStr2Bin
@strIP varchar(15),
@binIP binary(4) OUTPUT,
@Validate bit = 1
AS
IF @Validate = 1
BEGIN
-- only digits and dots
IF @strIP LIKE '%[^.0-9]%' RETURN (NULL)
-- number of dots must be 3
IF LEN(@strIP) - LEN(REPLACE(@strIP, '.', '')) != 3 RETURN (NULL)
-- all octets must be specified
IF @strIP NOT LIKE '%_%.%_%.%_%.%_%' RETURN (NULL)
END
DECLARE @oct1 binary(1),
@oct2 binary(1),
@oct3 binary(1),
@oct4 binary(1)
SET @oct1 = CAST(CAST(LEFT(@strIP, CHARINDEX('.', @strIP) - 1) AS int) AS
binary(1))
SET @oct2 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP) + 1,
CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) + 1) - CHARINDEX('.',
@strIP) - 1) AS int) AS binary(1))
SET @oct3 = CAST(CAST(SUBSTRING(@strIP, CHARINDEX('.', @strIP,
CHARINDEX('.', @strIP) + 1) + 1, (LEN(@strIP) - CHARINDEX('.',
REVERSE(@strIP)) + 1) - (CHARINDEX('.', @strIP, CHARINDEX('.', @strIP) +
1)) - 1) AS int) AS binary(1))
SET @oct4 = CAST(CAST(RIGHT(@strIP, LEN(@strIP) - (LEN(@strIP) -
CHARINDEX('.', REVERSE(@strIP)) + 1)) AS int) AS binary(1))
IF @Validate = 1
BEGIN
IF NOT(
(@oct1 BETWEEN 0x01 AND 0xFF) AND
(@oct2 BETWEEN 0x00 AND 0xFF) AND
(@oct3 BETWEEN 0x00 AND 0xFF) AND
(@oct4 BETWEEN 0x00 AND 0xFF)
) RETURN(NULL)
END
SET @binIP = @oct1 + @oct2 + @oct3 + @oct4
GO

利用例子:

DECLARE @binIP binary(4)
EXEC dbo.spIPAddrStr2Bin '172.29.23.2', @binIP OUTPUT, 0
PRINT @binIP

注意: 假如你不需求或不想考证自己的字符串IP地址,可以完好删掉对它们举行处理的代码
  以上是“若何将作为字符串保存的IP地址变成二进制数值?[MSSQL防范]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:

  • 若何将作为字符串保存的IP地址变成二进制数值?
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .