need help with unpacking ip address

M

Mike Wilson

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
 
T

ts

M> 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,364
Latest member
Stevanida

Latest Threads

Top