tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Articles > Java > Basics > How to convert Byte Array to Integer

How to convert Byte Array to Integer 

The following example shows converting a Byte array to Integer. First create a ByteBuffer with given byte array and then read next 4 bytes from it as an Integer.

java.nio.ByteBuffer
  • public static ByteBuffer wrap(byte[] array)
    Wraps a byte array into a buffer.
  • public int getInt()
    Reads the next four bytes in the ByteBuffer as an Integer.

File Name  :  
com/bethecoder/articles/basics/prim2bytea/ByteArray2IntegerTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.articles.basics.prim2bytea;

import java.nio.ByteBuffer;

public class ByteArray2IntegerTest {

  /**
   @param args
   */
  public static void main(String[] args) {

    //byte [] bytes = ByteBuffer.allocate(4).putInt(17291729).array();
    byte [] bytes = 17, -39, -47 };
    System.out.println(ByteBuffer.wrap(bytes).getInt());
  }

}
   

It gives the following output,
17291729



 
  


  
bl  br