Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should put it into a function like this:</p> <pre><code>public static int toInt(byte[] bytes, int offset) { int ret = 0; for (int i=0; i&lt;4 &amp;&amp; i+offset&lt;bytes.length; i++) { ret &lt;&lt;= 8; ret |= (int)bytes[i] &amp; 0xFF; } return ret; } </code></pre> <p>Example:</p> <pre><code>byte[] bytes = new byte[]{-2, -4, -8, -16}; System.out.println(Integer.toBinaryString(toInt(bytes, 0))); </code></pre> <p>Output:</p> <pre><code>11111110111111001111100011110000 </code></pre> <p>This takes care of running out of bytes and correctly handling negative byte values.</p> <p>I'm unaware of a standard function for doing this.</p> <p>Issues to consider:</p> <ol> <li><p><strong>Endianness:</strong> different CPU architectures put the bytes that make up an int in different orders. Depending on how you come up with the byte array to begin with you may have to worry about this; and</p></li> <li><p><strong>Buffering:</strong> if you grab 1024 bytes at a time and start a sequence at element 1022 you will hit the end of the buffer before you get 4 bytes. It's probably better to use some form of buffered input stream that does the buffered automatically so you can just use <code>readByte()</code> repeatedly and not worry about it otherwise;</p></li> <li><p><strong>Trailing Buffer:</strong> the end of the input may be an uneven number of bytes (not a multiple of 4 specifically) depending on the source. But if you create the input to begin with and being a multiple of 4 is "guaranteed" (or at least a precondition) you may not need to concern yourself with it.</p></li> </ol> <p>to further elaborate on the point of buffering, consider the <a href="http://java.sun.com/javase/6/docs/api/java/io/BufferedInputStream.html" rel="noreferrer"><code>BufferedInputStream</code></a>:</p> <pre><code>InputStream in = new BufferedInputStream(new FileInputStream(file), 1024); </code></pre> <p>Now you have an <code>InputStream</code> that <em>automatically</em> buffers 1024 bytes at a time, which is a lot less awkward to deal with. This way you can happily read 4 bytes at a time and not worry about too much I/O.</p> <p>Secondly you can also use <a href="http://java.sun.com/javase/6/docs/api/java/io/DataInputStream.html" rel="noreferrer"><code>DataInputStream</code></a>:</p> <pre><code>InputStream in = new DataInputStream(new BufferedInputStream( new FileInputStream(file), 1024)); byte b = in.readByte(); </code></pre> <p>or even:</p> <pre><code>int i = in.readInt(); </code></pre> <p>and not worry about constructing <code>int</code>s at all.</p>
    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