Integer to two digits hex in Java
Solution 1
String.format("%02X", value);
If you use X
instead of x
as suggested by aristar, then you don't need to use .toUpperCase()
.
Solution 2
Integer.toHexString(42);
Javadoc: http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#toHexString(int)
Note that this may give you more than 2 digits, however! (An Integer is 4 bytes, so you could potentially get back 8 characters.)
Here's a bit of a hack to get your padding, as long as you are absolutely sure that you're only dealing with single-byte values (255 or less):
Integer.toHexString(0x100 | 42).substring(1)
Many more (and better) solutions at Left padding integers (non-decimal format) with zeros in Java.
Solution 3
String.format("%02X", (0xFF & value));
Solution 4
Use Integer.toHexString()
. Dont forget to pad with a leading zero if you only end up with one digit. If your integer is greater than 255 you'll get more than 2 digits.
StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(myInt));
if (sb.length() < 2) {
sb.insert(0, '0'); // pad with leading zero if needed
}
String hex = sb.toString();
Solution 5
If you just need to print them try this:
for(int a = 0; a < 255; a++){
if( a % 16 == 0){
System.out.println();
}
System.out.printf("%02x ", a);
}
Salih Erikci
Suggest Board - Easy Feature Request Management Start getting new ideas from your users and customers. Perfect for your app, your product and service. Fill The Rest - Finish the sentences your way. Finish the half sentences your way.
Updated on July 08, 2020Comments
-
Salih Erikci almost 4 years
I need to change a integer value into 2-digit hex value in Java.Is there any way for this. Thanks
My biggest number will be 63 and smallest will be 0. I want a leading zero for small values.
-
Stealth Rabbi almost 11 yearsCan someone explain why this is doing a bit-wise and with 0xFF? This is why answers on SO shouldn't just be the code
-
aristar over 10 yearsOxFF & (byte)value just convert signed byte to unsigned. (int)-1 = 0xFFFFFFFF, (int)(-1 & 0xFf) = 0xFF
-
just lerning over 10 yearsIs the use of
StringBuilder
actually an improvement? pastebin.com/KyS06JMz -
David J. about 10 yearsThis is the best answer at present.
-
JRSofty about 10 yearsAgreed this one is much better than any of the others. It works perfectly. The only thing I would suggest is a little more description of the parameters passed to the method. I had to look up if I passed the value as an integer or as a hex string.
-
Grodriguez almost 10 yearsNice zero-padding hack :)
-
Freek de Bruijn about 9 yearsIn this case, the bit-wise and with 0xFF is not necessary because of the "%02X" format.
-
user823981 over 8 yearsYou can use the same technique for larger values. If you want padding for up to 2-byte values you can do Integer.toHexString( 0x10000 | value).substring(1). For N hex digits you append N 0s after the "1". Of course, your hex value must have no more than N digits.
-
plugwash over 7 yearsThe %02X specifier specifies a minimum of two digits. If you want exactly two digits (discarding higher bits) then you need the bitwise and.
-
tryp over 7 yearsGood to know, thanks for this additional information !
-
mrek over 2 yearsThis unfortunately does not work for strings, you have to use a little calculation hack