How to Write custom IP address algorithms and custom IP address Algorithms

Source: Internet
Author: User

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 */


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.