mysql ip address convert (ip2bigint,bigint2ip)

Convert IP address  to bigint





CREATE FUNCTION `F_Ip2Int`(ip varchar(15)) RETURNS bigint(20)
BEGIN
  declare tmp bigint default 0;
  while instr(ip,’.’)>0 do
    set tmp = tmp*256+ left(ip,instr(ip,’.’)-1);
    set ip = right(ip,length(ip)-instr(ip,’.’));
  end while;
  set tmp = tmp*256+ip;
  return tmp;
END

Convert bigint to IP Address




CREATE FUNCTION `F_Int2Ip`(iip bigint) RETURNS varchar(15)
BEGIN

  return concat((iip & 0xFF000000)>>24, ‘.’,
                (iip & 0x00FF0000)>>16, ‘.’,
                (iip & 0x0000FF00)>>8, ‘.’,
                iip & 0x000000FF);

END


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.