Say I have an array like this arr = [24, 21, 25, 40, 236, 89] which is meant to represent an IP address (first four bytes) and a port number (last 2 bytes), as in http://wiki.theory.org/BitTorrentSpecification#Tracker_Response, the binary peer section.
My question is this: how do I get the IP address and the port number from this?
arr[0..3].reverse.join '.' for the IP and arr[5] << 8 | arr[4] for the port doesn’t seem to be correct.
I’m assuming here that since I do string_from_tracker.unpack 'C*' to get arr, the bits are already in the native endianness…
I’m not super sure how this is even supposed to work.
Thanks.
What’s the “not correct part”? I.e. what’s the expected result?
At least here you have a bug. Combining the two last numbers should be done like so:
Which gives
23020. Seems more like a Bittorrent port number.Edit: Heh 🙂 Actually this is equivalent to the above: