title: Getting IP from DNS Response
body: <p>On a general level I'm trying to decode a DNS response. I've manage to make it as far as retrieving the names from the 'questions' section of the response but cannot manage to extract the IP addresses from the 'answers' section. I'm well aware of InetAddress.getByName() but thats not what I need. I need to figure out how to convert this set of bytes to an IP address...
  <img src="https://i.stack.imgur.com/zQt0k.png" alt="enter image description here"></p>
  
  <pre><code>private static void disectQuery(byte[] received) {
  
      ByteArrayInputStream bais = new ByteArrayInputStream(received);
      DataInputStream DataIS = new DataInputStream (bais);
  
      DNSResponse Response = new DNSResponse();
  
      try {
          Response.TID = DataIS.readShort();
          Response.Flags = DataIS.readShort();
          Response.NumQuestions = DataIS.readShort ();
          Response.NumAnswers = DataIS.readShort();
          Response.NumAuthorities = DataIS.readShort ();
          Response.NumAdditional = DataIS.readShort ();
  
          String rest = null;
          int questionsLeft = Response.NumQuestions;
          while(questionsLeft-- &gt; 0) {
              byte[] buffer = new byte[lastHostQueried.length()+1];
              DataIS.readFully (buffer);
              rest = new String(buffer, "latin1");
              int queryType = DataIS.readShort ();
              int queryClass = DataIS.readShort ();
          }
          int answersLeft = Response.NumAnswers;
          int i=13;
          while(i-- &gt; 0) {
              DataIS.readShort();
          }
          while(answersLeft-- &gt; 0) {
              ????
          }
  
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
  
  }
  </code></pre>