Pratap
Pratap

Reputation: 695

How to convert BigInteger to Hex of fixed length?

BigInteger.toString(16) does the work but sometimes the number of bytes of the hex varies. I need exactly 512 bytes hex everytime. I have also tried apache's Hex and DataTypeConverter and more. But none guarantees this consistency. Is there any other Java library available to generate a fixed length hex from a given BigInteger?

PS: I have searched stackoverflow before posting but found no discussion dealing with Fixed length hex issue.

Upvotes: 3

Views: 3176

Answers (1)

Emmanuel Lonca
Emmanuel Lonca

Reputation: 196

As given in comments, use String s = String.format("%0512x", BigInteger.valueOf(theNumber))

Formatter does support BigInteger: see its documentation here.

Upvotes: 10

Related Questions