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.

Salih Erikci
  • 5,076
  • 12
  • 39
  • 69

7 Answers7

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

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

Community
  • 1
  • 1
GabrielOshiro
  • 7,986
  • 4
  • 45
  • 57
  • 8
    This is the best answer at present. – David J. Feb 27 '14 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 '14 at 09:10
  • This unfortunately does not work for strings, you [have to use a little calculation hack](https://stackoverflow.com/a/15635584/3894977) – mrek Sep 30 '21 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 '22 at 18:02
32
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.

Community
  • 1
  • 1
ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • 3
    Nice zero-padding hack :) – Grodriguez May 09 '14 at 07: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 '15 at 22:55
31
String.format("%02X", (0xFF & value));    
Robert
  • 39,162
  • 17
  • 99
  • 152
aristar
  • 415
  • 4
  • 5
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();
Asaph
  • 159,146
  • 25
  • 197
  • 199
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);
}
java4loop
  • 41
  • 1
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));
}
0

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

String.format("0x%08X", 20) //print 0x00000014
Victor Choy
  • 4,006
  • 28
  • 35