Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> byte[] bytes = {-1, 0, 1, 2, 3 }; StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X ", b)); } System.out.println(sb.toString()); // prints "FF 00 01 02 03 " </code></pre> <h3>See also</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Formatter.html#syntax" rel="noreferrer"><code>java.util.Formatter</code> syntax</a> <ul> <li><code>%[flags][width]conversion</code> <ul> <li>Flag <code>'0'</code> - The result will be zero-padded </li> <li>Width <code>2</code></li> <li>Conversion <code>'X'</code> - The result is formatted as a hexadecimal integer, uppercase</li> </ul></li> </ul></li> </ul> <hr> <p>Looking at the text of the question, it's also possible that this is what is requested:</p> <pre><code> String[] arr = {"-1", "0", "10", "20" }; for (int i = 0; i &lt; arr.length; i++) { arr[i] = String.format("%02x", Byte.parseByte(arr[i])); } System.out.println(java.util.Arrays.toString(arr)); // prints "[ff, 00, 0a, 14]" </code></pre> <hr> <p>Several answers here uses <a href="http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#toHexString%28int%29" rel="noreferrer"><code>Integer.toHexString(int)</code></a>; this is doable, but with some caveats. Since the parameter is an <code>int</code>, a widening primitive conversion is performed to the <code>byte</code> argument, which involves sign extension.</p> <pre><code> byte b = -1; System.out.println(Integer.toHexString(b)); // prints "ffffffff" </code></pre> <p>The 8-bit <code>byte</code>, which is signed in Java, is sign-extended to a 32-bit <code>int</code>. To effectively undo this sign extension, one can mask the <code>byte</code> with <code>0xFF</code>.</p> <pre><code> byte b = -1; System.out.println(Integer.toHexString(b &amp; 0xFF)); // prints "ff" </code></pre> <p>Another issue with using <code>toHexString</code> is that it doesn't pad with zeroes:</p> <pre><code> byte b = 10; System.out.println(Integer.toHexString(b &amp; 0xFF)); // prints "a" </code></pre> <p>Both factors combined should make the <code>String.format</code> solution more preferrable.</p> <h3>References</h3> <ul> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.1" rel="noreferrer">JLS 4.2.1 Integral Types and Values</a> <ul> <li>For <code>byte</code>, from <code>-128</code> to <code>127</code>, inclusive</li> </ul></li> <li><a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.1.2" rel="noreferrer">JLS 5.1.2 Widening Primitive Conversion</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload