{"id":489,"date":"2008-03-24T00:00:00","date_gmt":"2008-03-23T16:00:00","guid":{"rendered":"http:\/\/www.strongd.net\/?p=489"},"modified":"2011-07-15T09:35:41","modified_gmt":"2011-07-15T01:35:41","slug":"mysql-ip-address-convert-ip2bigintbigint2ip","status":"publish","type":"post","link":"https:\/\/www.strongd.net\/?p=489","title":{"rendered":"mysql ip address convert (ip2bigint,bigint2ip)"},"content":{"rendered":"<p>Convert IP address&nbsp; to bigint<BR><br \/>\n<TABLE style=\"BORDER-RIGHT: #dcdcdc 1px dotted; PADDING-RIGHT: 5px; BORDER-TOP: #dcdcdc 1px dotted; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #dcdcdc 1px dotted; WIDTH: 96%; LINE-HEIGHT: 150%; PADDING-TOP: 5px; BORDER-BOTTOM: #dcdcdc 1px dotted; FONT-STYLE: italic\" cellSpacing=1 cellPadding=5 width=\"96%\" align=center border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD class=ash><I><BR>CREATE FUNCTION `F_Ip2Int`(ip varchar(15)) RETURNS bigint(20)<BR>BEGIN<BR>&nbsp;&nbsp;declare tmp bigint default 0;<BR>&nbsp;&nbsp;while instr(ip,&#8217;.&#8217;)&gt;0 do<BR>&nbsp;&nbsp;&nbsp;&nbsp;set tmp = tmp*256+ left(ip,instr(ip,&#8217;.&#8217;)-1);<BR>&nbsp;&nbsp;&nbsp;&nbsp;set ip = right(ip,length(ip)-instr(ip,&#8217;.&#8217;));<BR>&nbsp;&nbsp;end while;<BR>&nbsp;&nbsp;set tmp = tmp*256+ip;<BR>&nbsp;&nbsp;return tmp;<BR>END<BR><\/I><\/TD><\/TR><\/TBODY><\/TABLE><BR>Convert bigint to IP Address<br \/>\n<TABLE style=\"BORDER-RIGHT: #dcdcdc 1px dotted; PADDING-RIGHT: 5px; BORDER-TOP: #dcdcdc 1px dotted; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: #dcdcdc 1px dotted; WIDTH: 96%; LINE-HEIGHT: 150%; PADDING-TOP: 5px; BORDER-BOTTOM: #dcdcdc 1px dotted; FONT-STYLE: italic\" cellSpacing=1 cellPadding=5 width=\"96%\" align=center border=0><br \/>\n<TBODY><br \/>\n<TR><br \/>\n<TD class=ash><I><BR>CREATE FUNCTION `F_Int2Ip`(iip bigint) RETURNS varchar(15)<BR>BEGIN<BR><BR>&nbsp;&nbsp;return concat((iip &amp; 0xFF000000)&gt;&gt;24, &#8216;.&#8217;,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iip &amp; 0x00FF0000)&gt;&gt;16, &#8216;.&#8217;,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iip &amp; 0x0000FF00)&gt;&gt;8, &#8216;.&#8217;,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iip &amp; 0x000000FF);<BR><BR>END<BR><\/I><\/TD><\/TR><\/TBODY><\/TABLE><BR><br \/>\n<DIV><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Convert IP address&nbsp; to bigint CREATE FUNCTION `F_Ip2Int`(ip varchar(15)) RETURNS bigint(20)BEGIN&nbsp;&nbsp;declare tmp bigint default 0;&nbsp;&nbsp;while instr(ip,&#8217;.&#8217;)&gt;0 do&nbsp;&nbsp;&nbsp;&nbsp;set tmp = tmp*256+ left(ip,instr(ip,&#8217;.&#8217;)-1);&nbsp;&nbsp;&nbsp;&nbsp;set ip = right(ip,length(ip)-instr(ip,&#8217;.&#8217;));&nbsp;&nbsp;end while;&nbsp;&nbsp;set tmp = tmp*256+ip;&nbsp;&nbsp;return tmp;ENDConvert bigint to IP Address CREATE FUNCTION `F_Int2Ip`(iip bigint) RETURNS varchar(15)BEGIN&nbsp;&nbsp;return concat((iip &amp; 0xFF000000)&gt;&gt;24, &#8216;.&#8217;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iip &amp; 0x00FF0000)&gt;&gt;16, &#8216;.&#8217;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(iip &amp; 0x0000FF00)&gt;&gt;8, &#8216;.&#8217;,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iip &amp; 0x000000FF);END<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[102],"class_list":["post-489","post","type-post","status-publish","format-standard","hentry","category-mysql","tag-ip-address"],"_links":{"self":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/489","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489"}],"version-history":[{"count":1,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions"}],"predecessor-version":[{"id":880,"href":"https:\/\/www.strongd.net\/index.php?rest_route=\/wp\/v2\/posts\/489\/revisions\/880"}],"wp:attachment":[{"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.strongd.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}