Professional Web Applications Themes

need help with unpacking ip address - Ruby

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

  1. #1

    Default 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



    Mike Guest

  2. #2

    Default Re: need help with unpacking ip address

    On Wed, 24 Dec 2003 00:45:40 +0900, Mike Wilson <com>
    wrote:
     

    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
    Dennis Guest

  3. #3

    Default Re: need help with unpacking ip address

    >>>>> "M" == Mike Wilson <com> writes:

    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


    ts Guest

  4. #4

    Default Re: need help with unpacking ip address

    Thanks!

     
    >
    >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
    >[/ref]

    __________________________________________________ _______________
    Have fun customizing MSN Messenger learn how here!
    http://www.msnmessenger-download.com/tracking/reach_customize


    Mike Guest

Similar Threads

  1. #39304 [NEW]: Segmentation fault with list unpacking of string offset
    By dave at ramenlabs dot com in forum PHP Bugs
    Replies: 3
    Last Post: October 30th, 11:05 AM
  2. unpacking a tar-file
    By Marco in forum Ubuntu
    Replies: 7
    Last Post: June 2nd, 06:27 AM
  3. Error unpacking European Search Packverity_linux_eur.tar file
    By cfusion_mx_6.1_user in forum Coldfusion Server Administration
    Replies: 0
    Last Post: May 6th, 04:16 PM
  4. splitting / unpacking line into array
    By John McKown in forum PERL Beginners
    Replies: 3
    Last Post: February 4th, 01:01 AM
  5. Packing/unpacking
    By Julie in forum PERL Miscellaneous
    Replies: 2
    Last Post: September 22nd, 09:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139