From: Dennis Ranke Date: 2003-12-24T00:56:55+09:00 Subject: Re: need help with unpacking ip address On Wed, 24 Dec 2003 00:45:40 +0900, Mike Wilson 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