How to Write custom IP address algorithms and custom IP address Algorithms
Through the IP address, you can see the algorithm rule, write it as a custom IP address, or convert the IP address to a custom IP address. It can also be used to encrypt plaintext numbers. The following describes the positive and negative algorithms (in the unit of least one time ):
USE tempdbGOIF OBJECT_ID ('fn _ NrToChar ') is not null drop function fn_NrToCharGO /********************************* **************************************** **************************************** **************************************** * ****** % Function Name: fn_NrToChar % features: change the number to the character ************************************ **************************************** **************************************** **************************************** *** %: roy 2014-12-09 ************************************** **************************************** **************************************** **************************************** **/create function fn_NrToChar (@ Nr BIGINT, @ NrSystem BIGINT = 168 -- hexadecimal, @ Split VARCHAR (2) = '**' -- delimiter, @ PartCount BIGINT = 5 -- number of segments) returns varchar (50) as begindeclare @ s varchar (50) = ''while @ PartCount> 0 SELECT @ S = @ S + case when @ S> ''then @ Split ELSE ''END + RTRIM (@ Nr/POWER (@ NrSystem, @ PartCount), @ Nr = @ Nr % POWER (@ NrSystem, @ PartCount), @ PartCount = @ PartCount-1RETURN @ SENDGOIF OBJECT_ID ('fn _ CharToNr ') is not null drop function fn_CharToNrGO /********************************* **************************************** **************************************** **************************************** * ****** % Function Name: fn_CharToNr % features: change the character to a number ************************************ **************************************** **************************************** **************************************** *** %: roy 2014-12-09 ************************************** **************************************** **************************************** **************************************** **/create function fn_CharToNr (@ Str VARCHAR (50 ), @ NrSystem BIGINT = 168 -- hexadecimal, @ Split VARCHAR (2) = '**' -- delimiter, @ PartCount BIGINT = 5 -- number of segments) returns bigintas begindeclare @ Nr BIGINT = 0, @ StartLen TINYINT, @ StrLen TINYINTSELECT @ StrLen = LEN (@ Split ), @ StartLen = 1 WHILE @ PartCount> 0 SELECT @ Nr = @ Nr + SUBSTRING (@ Str, @ StartLen, CHARINDEX (@ Split, @ Str + @ Split, @ StartLen) -@ StartLen) * POWER (@ NrSystem, @ PartCount), @ StartLen = CHARINDEX (@ Split, @ Str + @ Split, @ StartLen) + @ StrLen, @ PartCount = @ PartCount-1RETURN @ NrENDGO -- test 1 (take IP address as an example) SELECT dbo. fn_CharToNr ('2017. 168.0.1 ', 256 ,'. ', 4) SELECT dbo. fn_NrToChar (827452293376,256 ,'. ', 4)/* 827452293376192.168.0.1 */go -- Test 2 DECLARE @ I BIGINT = 168 select power (@ I, 5) -- 133827821568 SELECT dbo. fn_CharToNr ('2017 ** 16 ** 1 ** 6 ** 07 ', DEFAULT, DEFAULT) SELECT dbo. fn_NrToChar (22361996620824, DEFAULT, DEFAULT)/* 22361996620824167 ** 16 ** 1 ** 6 ** 7 */