Thanks!
···
From: Dennis Ranke dennis.ranke@epost.de
Reply-To: ruby-talk@ruby-lang.org
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: need help with unpacking ip address
Date: Wed, 24 Dec 2003 00:56:55 +0900On Wed, 24 Dec 2003 00:45:40 +0900, Mike Wilson wmwilson01@hotmail.com >wrote:
I was wanting to write a quick little subnet calculator, but I’m having
trouble figuring out how to get the ip address to binary. Given
10.159.23.185, the right answer is “00001010.10011111.00010111.10111001”.
I try using “10.159.23.185”.unpack(“B8B8B8B8”), but receive [“00110001”,
“00110000”, “00101110”, “00110001”] as my answer, which obviously doesn’t
match up. What am I doing wrong?You are unpacking the the binary data of the string “10.159.23.185”. You
need to pack the 4 numbers into a 4 byte string first, for example like
this:irb(main):001:0> [10, 159, 23, 185].pack(‘C*’).unpack(‘B8B8B8B8’)
=> [“00001010”, “10011111”, “00010111”, “10111001”]–
exoticorn/farbrausch
Have fun customizing MSN Messenger learn how here!
http://www.msnmessenger-download.com/tracking/reach_customize