title: Java byte to string
body: <p>So I want to convert a byte array returned by the DatagramPacket's getData() function into a string. I know the way to convert the whole byte array to string is just:</p>
  
  <pre><code>String str = new String(bytes);
  </code></pre>
  
  <p>However, this prints out null characters at the end. So if the byte array was </p>
  
  <pre><code>[114, 101, 113, 117, 101, 115, 116, 0, 0, 0]
  </code></pre>
  
  <p>The 0's print out empty boxes on the console. So I basically only want to print out:</p>
  
  <pre><code>[114, 101, 113, 117, 101, 115, 116]
  </code></pre>
  
  <p>So I made this function:</p>
  
  <pre><code>public void print(DatagramPacket d) {
          byte[] data = d.getData();
          for(int i=0; i&lt; d.getLength(); i++) {
              if(data[i] != 0)
                  System.out.println( data[i] );
          }
      }
  </code></pre>
  
  <p>But unfortunately this is printing out the actual numbers instead of the letters. So how can I convert each individual byte to a string and print that out. Or if there is another way to print the byte array without the nulls at the end then that'll be fine too.</p>