Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > How to unpack an IP packed in little endian byte order

Reply
Thread Tools

How to unpack an IP packed in little endian byte order

 
 
Nicolas Vincent
Guest
Posts: n/a
 
      12-17-2009
Hello to forum members,

Here's my issue : I have IPs stored in a database but those are "little
endian byte order" encoded and the field type of the BDD is BigInt
(similar to Ruby's BigNum).

How could I get back my human readable IP ? I've been digging around
'unpack' for a while now but without success.

Here's an example :
BigNum stored in the database : 3232235797
Resulting IP : 192.168.1.21

Could you give me some ideas or code ? I'm really stucked !

Thanks for all,

Liteo
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      12-17-2009
2009/12/17 Nicolas Vincent <(E-Mail Removed)>:
> Hello to forum members,
>
> Here's my issue : I have IPs stored in a database but those are "little
> endian byte order" encoded and the field type of the BDD is BigInt
> (similar to Ruby's BigNum).
>
> How could I get back my human readable IP ? I've been digging around
> 'unpack' for a while now but without success.
>
> Here's an example :
> BigNum stored in the database : 3232235797
> Resulting IP : 192.168.1.21
>
> Could you give me some ideas or code ? I'm really stucked !


Do you mean like this:

irb(main):020:0> i
=> 3232235797
irb(main):021:0> ([i].pack("N")).unpack("C*")
=> [192, 168, 1, 21]
irb(main):022:0> ([i].pack("N")).unpack("C*").join(".")
=> "192.168.1.21"
irb(main):023:0>

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
 
Nicolas Vincent
Guest
Posts: n/a
 
      12-17-2009
Robert Klemme wrote:
> 2009/12/17 Nicolas Vincent <(E-Mail Removed)>:
>> BigNum stored in the database : 3232235797
>> Resulting IP : 192.168.1.21
>>
>> Could you give me some ideas or code ? I'm really stucked !

>
> Do you mean like this:
>
> irb(main):020:0> i
> => 3232235797
> irb(main):021:0> ([i].pack("N")).unpack("C*")
> => [192, 168, 1, 21]
> irb(main):022:0> ([i].pack("N")).unpack("C*").join(".")
> => "192.168.1.21"
> irb(main):023:0>
>
> Cheers
>
> robert


That's exactly it.

Thanks a lot Robert, you saved my day !
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Bertram Scharpf
Guest
Posts: n/a
 
      12-17-2009
Hi,

Am Donnerstag, 17. Dez 2009, 22:57:30 +0900 schrieb Nicolas Vincent:
> Here's an example :
> BigNum stored in the database : 3232235797
> Resulting IP : 192.168.1.21


Here's one without using pack/unpack:

ip = 3232235797
a = []
4.times { a.unshift ip & 0xff ; ip >>= 8 }
a.join "."
#=> "192.168.1.21"

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
*
Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.

 
Reply With Quote
 
Nicolas Vincent
Guest
Posts: n/a
 
      12-18-2009
Bertram Scharpf wrote:
> Hi,
>
> Am Donnerstag, 17. Dez 2009, 22:57:30 +0900 schrieb Nicolas Vincent:
>> Here's an example :
>> BigNum stored in the database : 3232235797
>> Resulting IP : 192.168.1.21

>
> Here's one without using pack/unpack:
>
> ip = 3232235797
> a = []
> 4.times { a.unshift ip & 0xff ; ip >>= 8 }
> a.join "."
> #=> "192.168.1.21"
>
> Bertram


Hi Bertram,

Thanks for your code explaining the underlying theory.

Regards,
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
utf-16 little endian byte order mark with libxml-ruby Tim Perrett Ruby 1 07-25-2007 12:50 PM
unpack signed short in network (big-endian) byte order baumanj@gmail.com Ruby 11 04-10-2006 03:19 PM
float: IEEE, big endian, little endian Ernst Murnleitner C++ 0 01-13-2004 01:48 PM
Big versus little endian (byte order) Gido Java 3 07-06-2003 08:36 PM
convert from BIG-ENDIAN to LITTLE-ENDIAN hicham C++ 2 07-02-2003 04:55 PM



Advertisments