Showing posts with label integer. Show all posts
Showing posts with label integer. Show all posts

Javascript hash functions to convert string into integer hash.

A collection of hash functions implemented in Javascript. Functions are useful for browser-side string processing, but as far as server side scripting in Javascript becoming more and more popular, these functions may be helpful for server side scripting too. if you just want to have a good hash function, and cannot wait, djb2 is one of the best string hash functions i know.


Erlang float to integer

Today I decided to write very small article. I think it may be interesting for programmers who came to Erlang from Java, C++ or C. From the point of view of Erlang developer everything is very simple. You just need to round or truncate the float and it will become an integer. Here is an example:

Eshell V5.8.4  (abort with ^G)
1> is_float(round(1.5)).
false
2> is_integer(round(1.5)).
true
3> is_float(trunc(1.5)).  
false
4> is_integer(trunc(1.5)).
true
5> round(1.5).            
2
6> trunc(1.5).            
1

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 :)