Hi, I want to compare two IP addresses so I want to convert them to long integers. Can anyone tell me how it can be done? I need to convert IP address to long and after I have to convert them back to IP address. Thanks in advance. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ashwin Kumar Sharma wrote:> Hi, > > I want to compare two IP addresses so I want to convert them to long > integers. Can anyone tell me how it can be done? I need to convert IP > address to long and after I have to convert them back to IP address. > > Thanks in advance.http://gtools.org/tool/ip-long-convert/ The above site might help u convert a static IP to long. But I don''t have an answer for a ruby code which converts an IP address into its long form. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ashwin Kumar Sharma wrote:> Hi, > > I want to compare two IP addresses so I want to convert them to long > integers. Can anyone tell me how it can be done? I need to convert IP > address to long and after I have to convert them back to IP address.IP address are 4 bytes represented in a dotted decimal format. Example: # 22.125.33.145 145 + (33 * 2**8) + (125 * 2**16) + (22 * 2**24) => 377299345 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Robert Walker wrote:> Ashwin Kumar Sharma wrote: >> Hi, >> >> I want to compare two IP addresses so I want to convert them to long >> integers. Can anyone tell me how it can be done? I need to convert IP >> address to long and after I have to convert them back to IP address. > > IP address are 4 bytes represented in a dotted decimal format. > > Example: > # 22.125.33.145 > 145 + (33 * 2**8) + (125 * 2**16) + (22 * 2**24) > => 377299345Thanks a lot Robert. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ashwin Kumar Sharma wrote:> Robert Walker wrote: >> Ashwin Kumar Sharma wrote: >>> Hi, >>> >>> I want to compare two IP addresses so I want to convert them to long >>> integers. Can anyone tell me how it can be done? I need to convert IP >>> address to long and after I have to convert them back to IP address. >> >> IP address are 4 bytes represented in a dotted decimal format. >> >> Example: >> # 22.125.33.145 >> 145 + (33 * 2**8) + (125 * 2**16) + (22 * 2**24) >> => 377299345 >Hi, I could convert the IP address to long format using the above logic, can any one tell how can I convert the long format back to IP address. I did search for the API I didn''t get any. Is there any API which does it in Rails or any logic to which can do it like above(Equivalent APIs for long2ip and ip2long PHP built in Rails). Thanks in advance. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>>>> Hi, >>>> >>>> I want to compare two IP addresses so I want to convert them to long >>>> integers. Can anyone tell me how it can be done? I need to convert IP >>>> address to long and after I have to convert them back to IP address. >>> >>> IP address are 4 bytes represented in a dotted decimal format. >>> >>> Example: >>> # 22.125.33.145 >>> 145 + (33 * 2**8) + (125 * 2**16) + (22 * 2**24) >>> => 377299345 >> > Hi, > > I could convert the IP address to long format using the above logic, can > any one tell how can I convert the long format back to IP address. I did > search for the API I didn''t get any. Is there any API which does it in > Rails or any logic to which can do it like above(Equivalent APIs for > long2ip and ip2long PHP built in Rails). > > Thanks in advance.Hi all, Thanks for your time, I could find out the solution for the above issue. Using this functions i converted implement ip2long and long2ip def ip2long(ip) long = 0 ip.split(/\./).each_with_index do |b, i| long += b.to_i << ( 8*i ) end long end def long2ip(long) ip = [] 4.times do |i| ip.push(long.to_i & 255) long = long.to_i >> 8 end ip.join(".") end If any one know any API to do this please get it. Regards. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.