title: decoding bytes to ipaddress
body: <p>I have a java application that listens to upd packets on a socket. I send it raw bytes that i then convert to ip addresses. The packet is composed of 9 bytes. 4 for the first address, then 1 for a mask and another 4 bytes for the second address.</p>
  
  <p>The strange thing that happens is that the 2 byte of the 2 address print the wrong value. why?</p>
  
  <pre><code>package net.floodlightcontroller.openlisp;
  
  import java.io.IOException;
  import java.net.DatagramPacket;
  import java.net.DatagramSocket;
  import java.net.InetAddress;
  import java.net.SocketException;
  
  import net.floodlightcontroller.packet.IPv4;
  
  public class UdpServerCp extends Thread {
  
      protected OFLISPRules oflr = OFLISPRules.getInstance();
      private static UdpServerCp instance;
      private int priority = 3;
  
      public static synchronized UdpServerCp getInstance() {
          if (instance == null)
          instance = new UdpServerCp();
          return instance;
      }
  
      public void run() {
  
          OpenlispHandler oh = OpenlispHandler.getInstance();
  
          byte[] receiveData = new byte[9];
          DatagramPacket receivePacket;
          System.out.println("Waiting to receive...");
          DatagramSocket serverSocket;
          try {
              serverSocket = new DatagramSocket(8888);
  
              while (true) {
                  receivePacket = new DatagramPacket(receiveData, receiveData.length);
  
                  serverSocket.receive(receivePacket);
  
                  // if i receive a packet and it doesn't already have a flow rule
                  // process it
                  if ((receivePacket != null) &amp;&amp; (oh.isNewOFRuleAdded() == false)){
                      byte[] bytes = receivePacket.getData();
  
  
                      //int eidInteger = ((int)bytes[3] &lt;&lt; 8*3) + ((int)bytes[2] &lt;&lt; 8*2) + ((int)bytes[1] &lt;&lt; 8) + ((int) bytes[0]);
                      int eidInteger = ((int)bytes[0] &lt;&lt; 8*3) + ((int)bytes[1] &lt;&lt; 8*2) + ((int)bytes[2] &lt;&lt; 8) + ((int) bytes[3]);
                      String eidAddressString = InetAddress.getByAddress(unpack(eidInteger)).getHostAddress();
                      System.out.println("buffer EID: " + eidAddressString);
  
                      int prefixInteger = (int) bytes[4];
                      System.out.println("buffer PREFIX: " + prefixInteger);
  
                      int rlocInteger = ((int)bytes[5] &lt;&lt; 8*3) + ((int)bytes[6] &lt;&lt; 8*2) + ((int)bytes[7] &lt;&lt; 8) + ((int) bytes[8]);
                      String rlocAddressString = InetAddress.getByAddress(unpack(rlocInteger)).getHostAddress();
                      System.out.println("buffer RLOC: " + rlocAddressString);
  
  
  
                      IPv4 eidADD = new IPv4();
                      IPv4 rlocADD = new IPv4();
                      eidADD.setDestinationAddress(eidAddressString);
                      rlocADD.setDestinationAddress(rlocAddressString);
  
                      oflr.rlocOFRule(eidADD, prefixInteger, rlocADD, priority);
                      oh.setNewOFRuleAdded(true);
                      this.priority++;
                  }
              }
          } catch (SocketException e1) {
              e1.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      byte[] unpack(int bytes) {
          return new byte[] { 
                  (byte) ((bytes &gt;&gt;&gt; 24) &amp; 0xff),
                  (byte) ((bytes &gt;&gt;&gt; 16) &amp; 0xff), 
                  (byte) ((bytes &gt;&gt;&gt; 8) &amp; 0xff), 
                  (byte) ((bytes) &amp; 0xff) };
      }
  }
  </code></pre>