# Inet\_addr inet\_ntoa

**URL:** <https://www.ruby-forum.com/t/inet-addr-inet-ntoa/105530>\
**Category:** Ruby\
**Created:** [May 30, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/inet-addr-inet-ntoa/105530 "2007-05-30T20:17:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9e8a1a/32.png) [@rickf](https://www.ruby-forum.com/u/rickf)\
**Post date:** [May 30, 2007, 8:17pm UTC](https://www.ruby-forum.com/t/inet-addr-inet-ntoa/105530/1 "2007-05-30T20:17:56Z")

</div>

Hi All,

I tried looking up answers for this with little success…any help is  
appreciated, bear with my newness to Ruby.

The application I am working on can store ip addresses in a DB. They are  
accessed by different platforms so the plan is to store IPs in network  
order and display as host order.

I cannot find the following routines in ruby land to aid with the  
process:

inet\_addr - to store the IP as network ordered  
inet\_ntoa - to retrieve the IP as host ordered

How do people deal with these conversions? I assume dealing with Endian  
issues affects several of you folks?

Any help is greatly appreciated…sorry if this has been covered before.

Thanks,  
Rick

---

<div class="post-metadata">

**Author:** ![rickf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9e8a1a/32.png) [@rickf](https://www.ruby-forum.com/u/rickf)\
**Post date:** [May 30, 2007, 8:45pm UTC](https://www.ruby-forum.com/t/inet-addr-inet-ntoa/105530/2 "2007-05-30T20:45:12Z")

</div>

Hi

On 5/30/07, Rick F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I cannot find the following routines in ruby land to aid with the  
> process:
> 
> inet\_addr - to store the IP as network ordered

You can get the ip in network order using Socket.gethostbyname  
([http://www.ruby-doc.org/core/classes/Socket.html#M002122](http://www.ruby-doc.org/core/classes/Socket.html#M002122))

---

<div class="post-metadata">

**Author:** ![rickf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/9e8a1a/32.png) [@rickf](https://www.ruby-forum.com/u/rickf)\
**Post date:** [May 30, 2007, 10:19pm UTC](https://www.ruby-forum.com/t/inet-addr-inet-ntoa/105530/3 "2007-05-30T22:19:26Z")

</div>

On Thu, May 31, 2007 at 03:17:57AM +0900, Rick F. wrote:

> process:
> 
> inet\_addr - to store the IP as network ordered  
> inet\_ntoa - to retrieve the IP as host ordered
> 
> How do people deal with these conversions? I assume dealing with Endian  
> issues affects several of you folks?
> 
> Any help is greatly appreciated…sorry if this has been covered before.

Look at:

ri Array#pack  
ri String#unpack

In particular, conversion “N” will pack as four bytes in network order  
(big-endian)
