·您的位置: 首页 » 资源教程 » 编程开发 » 数据库 » 如何将作为字符串保存的IP地址变为二进制数值?

如何将作为字符串保存的IP地址变为二进制数值?

类别: MSSQL教程  评论数:0 总得分:0
对于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地址,可以完全删掉对它们进行处理的代码。
-= 资 源 教 程 =-
文 章 搜 索
关键词:
类型:
范围:
纯粹空间 softpure.com
Copyright © 2006-2008 暖阳制作 版权所有
QQ: 15242663 (拒绝闲聊)  Email: faisun@sina.com
 纯粹空间 - 韩国酷站|酷站欣赏|教程大全|资源下载|免费博客|美女壁纸|设计素材|技术论坛   Valid XHTML 1.0 Transitional
百度搜索 谷歌搜索 Alexa搜索 | 粤ICP备19116064号-1