Parsing and Formatting a Byte Array into Octal in Java

Java 8Object Oriented ProgrammingProgramming

Set a BigInteger object.

BigInteger one;

Now, create a ByteArray.

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

For Octal, we have used 8 as the toString() method parameter.

String strResult = one.toString(8);

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);
      // Octal
      String strResult = one.toString(8);
      System.out.println("ByteArray to Octal = "+strResult);
   }
}

Output

ByteArray to Octal = 200000
raja
Published on 20-Dec-2018 10:31:20
Advertisements