Erlang convert IP to integer & integer to IP.

Jut needed to convert ip to integer for Erlang project and have not found code via Google. So decided to post it here for anybody who will find it helpful.

First function converting IP tuple to integer:

ip_to_int({A,B,C,D}) -> (A*16777216)+(B*65536)+(C*256)+(D).

Code to convert integer to IP tuple:

int_to_ip(Ip)->	{Ip bsr 24, (Ip band 16711680) bsr 16, (Ip band 65280) bsr 8, Ip band 255}.

And brief dumb example:

Eshell V5.8.4  (abort with ^G)
1> Ip = {1,2,3,4}.
{1,2,3,4}
2> db_server:int_to_ip(db_server:ip_to_int(Ip)).
{1,2,3,4}
3> 

Hope this code helped you, if you have reached this page :) Ye can also read how to convert string to Erlang tuple and Erlang tuple to string.

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)
Tags: , ,