Creating IP and Port from Byte Buffer

I have a byte buffer 6 bytes long, the first four contains the ip address, the last 2 contains the port, in notes with large ends.

to get the ip that I use

(apply str (interleave (map int (take 4 peer)) (repeat ".")))

Does the byte set int int to get the IP address?

as well as in java i,

    int port = 0;
    port |= peerList[i+4] & 0xFF;
    port <<= 8;
    port |= peerList[i+5] & 0xFF;

this snippet to get the port address. How can I convert this to clojure?

+3
source share
1 answer

yes matching them should be safe in this case, because any leading zeros that are entered by writing to a larger data type are deleted again when it is converted to a string

The second part is much easier because you start with a list of bytes.

(+ (* 256 (nth 5 peer)) (nth 4 peer))

,

(defn bytes-to-num [bytes] 
     (let [powers (iterate #(* % 256) 1)]
       (reduce + 0 (map * bytes powers))))
+2

Source: https://habr.com/ru/post/1724566/


All Articles