title: convert byte array to string in java
body: <p>I try to convert byte array to string in java using <code>new String( bytes, "UTF-8")</code> method, but they only return the object. like this @AB4634bSbbfa</p>
  
  <p>So, I searched some way to solve this problem.</p>
  
  <p>I finally get valid string array, by converting hex-code to basic-character array.
  like this. <code>char[] chars = {"0", "1", ... "e", "f"};</code></p>
  
  <p>This never happened before why do i have to convert hex-code to get valid string.</p>
  
  <p>Here is method.
  byte array which is hashed by <code>Mac-sha-256</code> with specific key when i hashed.</p>
  
  <pre><code>    public static String getHashString() {
          String algorithm = "HmacSHA256";
  
          String hashKey = "some_key";
          String message = "abcdefg";
  
          String hexed = "";
  
          try {
              Mac sha256_HMAC = Mac.getInstance(algorithm);
              SecretKeySpec secret_key = new SecretKeySpec(hashKey.getBytes(), algorithm);
              sha256_HMAC.init(secret_key);
  
              byte[] hash = sha256_HMAC.doFinal(message.getBytes("UTF-8"));
  
              // it doesn't work for me.
  //            hexed = new String(hash, "UTF-8");
  
              // it works.
              hexed = bytesToHex(hash);
  
          } catch (NoSuchAlgorithmException e) {
              e.printStackTrace();
          } catch (InvalidKeyException e) {
              e.printStackTrace();
          } catch (UnsupportedEncodingException e) {
              e.printStackTrace();
          }
  
          return hexed;
      }
  
      public static final char[] HEX_DIGITS = "0123456789abcdef".toCharArray();
      public static String bytesToHex(final byte[] data ) {
          final int l = data.length;
          final char[] hexChars = new char[l&lt;&lt;1];
          for( int i=0, j =0; i &lt; l; i++ ) {
              hexChars[j++] = HEX_DIGITS[(0xF0 &amp; data[i]) &gt;&gt;&gt; 4];
              hexChars[j++] = HEX_DIGITS[0x0F &amp; data[i]];
          }
          return new String(hexChars);
      }
  </code></pre>
  
  <p>Thanks.</p>