How to unpack an IP packed in little endian byte order

Discussion in 'Ruby' started by Nicolas Vincent, Dec 17, 2009.

  1. Hello to forum members,

    Here's my issue : I have IPs stored in a database but those are "little
    endian byte order" encoded and the field type of the BDD is BigInt
    (similar to Ruby's BigNum).

    How could I get back my human readable IP ? I've been digging around
    'unpack' for a while now but without success.

    Here's an example :
    BigNum stored in the database : 3232235797
    Resulting IP : 192.168.1.21

    Could you give me some ideas or code ? I'm really stucked !

    Thanks for all,

    Liteo
    --
    Posted via http://www.ruby-forum.com/.
     
    Nicolas Vincent, Dec 17, 2009
    #1
    1. Advertising

  2. 2009/12/17 Nicolas Vincent <>:
    > Hello to forum members,
    >
    > Here's my issue : I have IPs stored in a database but those are "little
    > endian byte order" encoded and the field type of the BDD is BigInt
    > (similar to Ruby's BigNum).
    >
    > How could I get back my human readable IP ? I've been digging around
    > 'unpack' for a while now but without success.
    >
    > Here's an example :
    > BigNum stored in the database : 3232235797
    > Resulting IP : 192.168.1.21
    >
    > Could you give me some ideas or code ? I'm really stucked !


    Do you mean like this:

    irb(main):020:0> i
    => 3232235797
    irb(main):021:0> (.pack("N")).unpack("C*")
    => [192, 168, 1, 21]
    irb(main):022:0> (.pack("N")).unpack("C*").join(".")
    => "192.168.1.21"
    irb(main):023:0>

    Cheers

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
     
    Robert Klemme, Dec 17, 2009
    #2
    1. Advertising

  3. Robert Klemme wrote:
    > 2009/12/17 Nicolas Vincent <>:
    >> BigNum stored in the database : 3232235797
    >> Resulting IP : 192.168.1.21
    >>
    >> Could you give me some ideas or code ? I'm really stucked !

    >
    > Do you mean like this:
    >
    > irb(main):020:0> i
    > => 3232235797
    > irb(main):021:0> (.pack("N")).unpack("C*")
    > => [192, 168, 1, 21]
    > irb(main):022:0> (.pack("N")).unpack("C*").join(".")
    > => "192.168.1.21"
    > irb(main):023:0>
    >
    > Cheers
    >
    > robert


    That's exactly it.

    Thanks a lot Robert, you saved my day !
    --
    Posted via http://www.ruby-forum.com/.
     
    Nicolas Vincent, Dec 17, 2009
    #3
  4. Hi,

    Am Donnerstag, 17. Dez 2009, 22:57:30 +0900 schrieb Nicolas Vincent:
    > Here's an example :
    > BigNum stored in the database : 3232235797
    > Resulting IP : 192.168.1.21


    Here's one without using pack/unpack:

    ip = 3232235797
    a = []
    4.times { a.unshift ip & 0xff ; ip >>= 8 }
    a.join "."
    #=> "192.168.1.21"

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    *
    Discover String#notempty? at <http://raa.ruby-lang.org/project/step>.
     
    Bertram Scharpf, Dec 17, 2009
    #4
  5. Bertram Scharpf wrote:
    > Hi,
    >
    > Am Donnerstag, 17. Dez 2009, 22:57:30 +0900 schrieb Nicolas Vincent:
    >> Here's an example :
    >> BigNum stored in the database : 3232235797
    >> Resulting IP : 192.168.1.21

    >
    > Here's one without using pack/unpack:
    >
    > ip = 3232235797
    > a = []
    > 4.times { a.unshift ip & 0xff ; ip >>= 8 }
    > a.join "."
    > #=> "192.168.1.21"
    >
    > Bertram


    Hi Bertram,

    Thanks for your code explaining the underlying theory.

    Regards,
    --
    Posted via http://www.ruby-forum.com/.
     
    Nicolas Vincent, Dec 18, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gido
    Replies:
    3
    Views:
    5,854
  2. hicham
    Replies:
    2
    Views:
    9,027
    dxcoder
    Jul 2, 2003
  3. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    869
    Ernst Murnleitner
    Jan 13, 2004
  4. Replies:
    11
    Views:
    382
  5. Tim Perrett
    Replies:
    1
    Views:
    216
    Tim Perrett
    Jul 25, 2007
Loading...

Share This Page