21

I have to convert an int to an hex value. This is for example the int value:

int_value = -13516;

To convert to a hex value i do:

hex_value = Integer.toHexString(int_value);

The value that I should get is : -34CC (I don't know if i should make it positive).

The thing is that doing the conversion that way, the value that I get is: ffff cb34

Can't I use this function to make this conversion?

masmic
  • 3,526
  • 11
  • 52
  • 105
  • It did make the conversion! It gave you the hexadecimal representation of the value, which has the sign bit set because it's negative. Android uses two's complement to represent negative integers: a negative value is represented by the leftmost bit being set, and a positive value is represented by the leftmost bit being unset. See [Signed number representations](https://en.wikipedia.org/wiki/Signed_number_representations). I realize this is two years old, but a solid grasp of the underlying mechanisms is a valuable thing for programmers to have. :) – Lorne Laliberte Oct 28 '15 at 16:51
  • @LorneLaliberte It's still unexpected if (for example) you have a C background, where printf's %X format specifier would give you an unsigned hexadecimal representation of whatever number you feed it, signed or not. Worse still, Java doesn't even *have* unsigned integer types, so you can't even fix it that way! – Michael Feb 14 '16 at 21:51
  • @Micheal there is no difference between C and Java in this case, in fact java is also returning an unsigned value. The bits (and thus the hexadecimal representation of those bits) are the same either way. "Signed" and "unsigned" are just different interpretations of the bits. I think you might want to refresh your memory of C's `sprintf` function, as the `%X` specifier works the same way in both languages. – Lorne Laliberte Feb 15 '16 at 23:11

6 Answers6

42

Documentation says Integer.toHexString returns the hexadecimal representation of the int as an unsigned value.

I believe Integer.toString(value, 16) will accomplish what you want.

Community
  • 1
  • 1
arnefm
  • 1,008
  • 8
  • 7
3
public static int convert(int n) {
  return Integer.valueOf(String.valueOf(n), 16);
}
 // in onstart:
 Log.v("TAG", convert(20) + "");  // 32
 Log.v("TAG", convert(54) + "");  // 84

From: Java Convert integer to hex integer

Community
  • 1
  • 1
VM4
  • 6,321
  • 5
  • 37
  • 51
  • But making this way, aren't you doing the conversion from hex to int? I need to make it other way, from a signed int, to a hex. – masmic Aug 06 '13 at 11:21
3

Both Integer.toHexString, as well as String.format("%x") do not support signs. To solve the problem, you can use a ternary expression:

    int int_value = -13516;
    String hex_value = int_value < 0
                       ? "-" + Integer.toHexString(-int_value)
                       : Integer.toHexString(int_value);
Kurt Huwig
  • 983
  • 11
  • 11
3
String.format("#%06X", (0xFFFFFF & colorYellow));

Output: #FFC107

Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
2

Go through following code for Integer to hex and Hex to integer Conversion

public class MainActivity extends Activity {

int number;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    number = 678668;
    Log.i("ACT", "Integer Number  " + number);

    /**
     * Code for convert integer number to hex number. two mwthods.
     */
    Log.i("ACT", String.format("#%x", number)); // use lower case x for
                                                // lowercase hex
    Log.i("ACT", "#" + Integer.toHexString(number));

    /**
     * Code for convert hex number to integer number
     */
    String hex = Integer.toHexString(number).replace("/^#/", "");
    int intValue = Integer.parseInt(hex, 16);

    Log.i("ACT", "Integer Number  " + intValue);
   }

}
mdDroid
  • 3,135
  • 2
  • 22
  • 34
1

I don't think the above answers would give you the exact value for the signed bits. For example the value of 11 is 0B but the value of -11 would be F5 and not -B since 2's complement gets into the game to solve this i have modified the above answer

int int_value = -11;
 String hex_value = int_value < 0
                           ? Integer.toHexString(int_value+65536) :    Integer.toHexString(int_value);
 String shortHexString = hex_value.substring(2); 

where, 65536 is 2^16 now you can get the expected results . Happy coding :)

  • List item
Abhilash
  • 500
  • 4
  • 13