Integer to two digits hex in Java

JavaIntegerHex

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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 - Java

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 https://stackoverflow.com/questions/3149692/left-padding-integers-non-decimal-format-with-zeros-in-java.

Solution 3 - Java

String.format("%02X", (0xFF & value));    

Solution 4 - Java

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 - Java

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

Solution 6 - Java

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

Solution 7 - Java

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSalih ErikciView Question on Stackoverflow
Solution 1 - JavaGabrielOshiroView Answer on Stackoverflow
Solution 2 - JavaziesemerView Answer on Stackoverflow
Solution 3 - JavaaristarView Answer on Stackoverflow
Solution 4 - JavaAsaphView Answer on Stackoverflow
Solution 5 - Javajava4loopView Answer on Stackoverflow
Solution 6 - JavaMohamed Abd El RaoufView Answer on Stackoverflow
Solution 7 - JavaVictor ChoyView Answer on Stackoverflow