58

I'm trying to convert a number from an integer into an another integer which, if printed in hex, would look the same as the original integer.

For example:

Convert 20 to 32 (which is 0x20)

Convert 54 to 84 (which is 0x54)

user1215143
  • 583
  • 1
  • 4
  • 4
  • Possible duplicate of [How to get hex value from integer in java](http://stackoverflow.com/questions/5258415/how-to-get-hex-value-from-integer-in-java) – Vadzim Dec 17 '16 at 10:47
  • I just realised now that to actually solve the given test cases, it is to convert **hex to int**, not the other way around. However, the question title that says "Java Convert integer to hex integer" has led many answers including mine and the most upvoted one to converting **int to hex**. – Wit Jun 23 '17 at 01:41

8 Answers8

256

The easiest way is to use Integer.toHexString(int)

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
Ajith
  • 2,585
  • 2
  • 13
  • 3
  • 5
    The question was to convert from integer to integer, not integer to string. Please read the question again. – Adam Nybäck Nov 01 '13 at 08:11
  • 38
    That doesn't make sense though, you can't control the integer's internal representation. If you want something in hex, you're by definition asking about a _human readable_ representation. – sircodesalot Mar 06 '14 at 15:45
  • 4
    @sircodesalot: The question is about calculating an integer with a certain property _if_ printed as hex, which is a purely mathematical conversion. I strongly disagree with Jossef that an answer should be marked as correct based on google results, but according to the votes, I seem to be in the minority. – Pianosaurus Jun 24 '16 at 11:17
  • @AdamNybäck rekt – Sipty Feb 14 '18 at 16:30
42
public static int convert(int n) {
  return Integer.valueOf(String.valueOf(n), 16);
}

public static void main(String[] args) {
  System.out.println(convert(20));  // 32
  System.out.println(convert(54));  // 84
}

That is, treat the original number as if it was in hexadecimal, and then convert to decimal.

João Silva
  • 89,303
  • 29
  • 152
  • 158
  • 3
    Converting from 4 bytes to a string, then from string to an int and then to hex? No, thanks. – Ondra Žižka Jun 06 '13 at 19:32
  • 3
    This solution is flawed. Try to run the following: Integer.valueOf(String.valueOf(-2115381772), 16) - this returns a NumberFormatException. – Lonzak Oct 15 '13 at 07:19
  • 1
    This converts a number in base 16 to its representation in base 10. [Correct answer is below](http://stackoverflow.com/a/9321597/2823516) – Joaquin Iurchuk Oct 24 '15 at 21:26
13

Another way to convert int to hex.

String hex = String.format("%X", int);

You can change capital X to x for lowercase.

Example:

String.format("%X", 31) results 1F.

String.format("%X", 32) results 20.

Wit
  • 605
  • 5
  • 17
  • `String hex = String.format("%02X", int);` for fixed length (two characters with eventual leading zeros). – qwert2003 Jan 20 '20 at 01:11
7
int orig = 20;
int res = Integer.parseInt(""+orig, 16);
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
5

You could try something like this (the way you would do it on paper):

public static int solve(int x){
    int y=0;
    int i=0;

    while (x>0){
        y+=(x%10)*Math.pow(16,i);
        x/=10;
        i++;
    }
    return y;
}

public static void main(String args[]){
    System.out.println(solve(20));
    System.out.println(solve(54));
}

For the examples you have given this would calculate: 0*16^0+2*16^1=32 and 4*16^0+5*16^1=84

ppalasek
  • 372
  • 2
  • 12
4
String input = "20";
int output = Integer.parseInt(input, 16); // 32
driangle
  • 11,601
  • 5
  • 47
  • 54
1

The following is optimized iff you only want to print the hexa representation of a positive integer.

It should be blazing fast as it uses only bit manipulation, the utf-8 values of ASCII chars and recursion to avoid reversing a StringBuilder at the end.

public static void hexa(int num) {
    int m = 0;
    if( (m = num >>> 4) != 0 ) {
        hexa( m );
    }
    System.out.print((char)((m=num & 0x0F)+(m<10 ? 48 : 55)));
}
Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

Simply do this:

public static int specialNum(num){

    return Integer.parseInt( Integer.toString(num) ,16)
}

It should convert any special decimal integer to its hexadecimal counterpart.

ed9w2in6
  • 133
  • 7