95

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.

3
  • You need to print it? you need to turn it into a string? Dec 31, 2011 at 17:34
  • i will write into file as two-digit hex Dec 31, 2011 at 17:36
  • Have you at least checked the Integer javadoc? How do you plan to convert 12345656 to a 2-digit hex value?
    – JB Nizet
    Dec 31, 2011 at 17:36

7 Answers 7

171
String.format("%02X", value);

If you use X instead of x as suggested by aristar, then you don't need to use .toUpperCase().

4
  • 8
    This is the best answer at present.
    – David J.
    Feb 27, 2014 at 22:49
  • 1
    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.
    – JRSofty
    Mar 25, 2014 at 9:10
  • This unfortunately does not work for strings, you have to use a little calculation hack
    – mrek
    Sep 30, 2021 at 15:20
  • 1
    I know this is old, but using this method of conversion is extremely slow. If you're doing it in a loop at all, I recommend not using String.format.
    – Elec0
    Sep 21, 2022 at 18:02
33
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.

2
  • 3
    Nice zero-padding hack :)
    – Grodriguez
    May 9, 2014 at 7:45
  • 1
    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.
    – user823981
    Sep 16, 2015 at 22:55
31
String.format("%02X", (0xFF & value));    
5
  • 8
    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 Jul 26, 2013 at 11:48
  • 4
    OxFF & (byte)value just convert signed byte to unsigned. (int)-1 = 0xFFFFFFFF, (int)(-1 & 0xFf) = 0xFF
    – aristar
    Aug 23, 2013 at 18:26
  • In this case, the bit-wise and with 0xFF is not necessary because of the "%02X" format. May 5, 2015 at 14:34
  • 2
    The %02X specifier specifies a minimum of two digits. If you want exactly two digits (discarding higher bits) then you need the bitwise and.
    – plugwash
    Nov 16, 2016 at 13:15
  • Good to know, thanks for this additional information !
    – tryp
    Jan 10, 2017 at 15:21
14

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();
1
4

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);
}
2

According to GabrielOshiro, If you want format integer to length 8, try this

String.format("0x%08X", 20) //print 0x00000014
1

i use this to get a string representing the equivalent hex value of an integer separated by space for every byte EX : hex val of 260 in 4 bytes = 00 00 01 04

    public static String getHexValString(Integer val, int bytePercision){
    StringBuilder sb = new StringBuilder();
    sb.append(Integer.toHexString(val));

    while(sb.length() < bytePercision*2){
        sb.insert(0,'0');// pad with leading zero
    }

    int l = sb.length(); // total string length before spaces
    int r = l/2; //num of rquired iterations

    for (int i=1; i < r;  i++){
        int x = l-(2*i); //space postion
        sb.insert(x, ' ');
    }
    return sb.toString().toUpperCase();         
}

public static void main(String []args){
    System.out.println("hex val of 260 in 4 bytes = " + getHexValString(260,4));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.