Need help with unpacking ip address

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?

Thanks!

···

Tired of slow downloads? Compare online deals from your local high-speed
providers now. https://broadband.msn.com

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”]

···

On 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?


exoticorn/farbrausch

10.159.23.185, the right answer is "00001010.10011111.00010111.10111001".

one way to do it

svg% ruby -e 'p ("%08b." * 4 % "10.159.23.185".split(/\./)).chop'
"00001010.10011111.00010111.10111001"
svg%

Guy Decoux