Integer to two digits hex in Java

96,019

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);
}
Share:
96,019
Salih Erikci
Author by

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, 2020

Comments

  • Salih Erikci
    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
    Stealth Rabbi almost 11 years
    Can 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
    aristar over 10 years
    OxFF & (byte)value just convert signed byte to unsigned. (int)-1 = 0xFFFFFFFF, (int)(-1 & 0xFf) = 0xFF
  • just lerning
    just lerning over 10 years
    Is the use of StringBuilder actually an improvement? pastebin.com/KyS06JMz
  • David J.
    David J. about 10 years
    This is the best answer at present.
  • JRSofty
    JRSofty about 10 years
    Agreed 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
    Grodriguez almost 10 years
    Nice zero-padding hack :)
  • Freek de Bruijn
    Freek de Bruijn about 9 years
    In this case, the bit-wise and with 0xFF is not necessary because of the "%02X" format.
  • user823981
    user823981 over 8 years
    You 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
    plugwash over 7 years
    The %02X specifier specifies a minimum of two digits. If you want exactly two digits (discarding higher bits) then you need the bitwise and.
  • tryp
    tryp over 7 years
    Good to know, thanks for this additional information !
  • mrek
    mrek over 2 years
    This unfortunately does not work for strings, you have to use a little calculation hack