Parsing and Formatting a Byte Array into Decimal in Java


Set a BigInteger object.

BigInteger one;

Now, create a ByteArray −

byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };
one = new BigInteger(byteArr);

For Decimal, we have used toString() method without any parameter value.

String strResult = one.toString();

The following is an example −

Example

 Live Demo

import java.math.*;
public class Demo {
   public static void main(String[] args) {
      BigInteger one;
      // ByteArray
      byte byteArr[] = new byte[] { 0x1, 0x00, 0x00 };
      one = new BigInteger(byteArr);
      // Decimal
      String strResult = one.toString();
      System.out.println("ByteArray to Decimal = "+strResult);
   }
}

Output

ByteArray to Decimal = 65536

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements