title: Convert from hex to int and vice versa in java
body: <p>I have next method for converting to int from hex:</p>
  <pre><code>public static byte[] convertsHexStringToByteArray2(String hexString) {
      int hexLength = hexString.length() / 2;
      byte[] bytes = new byte[hexLength];
      for (int i = 0; i &lt; hexLength; i++) {
          int hex = Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
          bytes[i] = (byte) hex;
      }
  
      return bytes;
  }
  </code></pre>
  <p>when I use it for &quot;80&quot; hex string I get strange, not expected result:</p>
  <pre><code>public static void main(String[] args) {
      System.out.println(Arrays.toString(convertsHexStringToByteArray2(&quot;80&quot;))); // gives -128
      System.out.println(Integer.toHexString(-128));
      System.out.println(Integer.toHexString(128));
  }
  </code></pre>
  <p>the output:</p>
  <pre><code>[-128]
  ffffff80
  80
  </code></pre>
  <p>I expect that &quot;80&quot; will be 128. What is wrong with my method?</p>