Convert a Byte Array into Hexadecimal through BigInteger in Java
Description
The following code shows how to convert a Byte Array into Hexadecimal through BigInteger.
Example
/*from w w w. ja va2s.c o m*/
import java.math.BigInteger;
public class Main {
public static void main(String[] argv) throws Exception {
byte[] bytes = new byte[] { (byte) 0x12, (byte) 0x0F, (byte) 0xF0 };
BigInteger bi = new BigInteger(bytes);
// Format to hexadecimal
String s = bi.toString(16);
if (s.length() % 2 != 0) {
s = "0" + s;
}
System.out.println(s);
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »