Reputation: 3564
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?
Upvotes: 22
Views: 49618
Reputation: 13
The question has hidden mistake: if we are talking about four Hex digits that doesn't mean int
(4 bytes or 8 Hex digits) but short
in Java.
If we are talking about int
we must keep 8 Hex digits. The upper example gives wrong transformation with big numbers of negative int
.
// Convert int to Hex String
int value = -1878724255; //-13516; //
String hexString = String.format("%08X", value);
System.out.println("1: " + hexString);
// this is wrong for an int
int value2 = (value < 0) ? value + 65536 : value;
System.out.println("2: " + Integer.toString(value2, 16));
// Correct way to handle negative values of int is adding 2^32 (4294967296)
long unsignedValue = value & 0xFFFFFFFFL;
System.out.println("3: " + Long.toString(unsignedValue, 16));
the result:
1: 9004F161
2: -6ffa0e9f
3: 9004f161
Upvotes: 0
Reputation: 22394
String.format("#%06X", (0xFFFFFF & colorYellow));
Output: #FFC107
Upvotes: 5
Reputation: 1008
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.
Upvotes: 42
Reputation: 507
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 :)
Upvotes: 1
Reputation: 1013
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);
Upvotes: 3
Reputation: 6501
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
Upvotes: 3
Reputation: 3195
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);
}
}
Upvotes: 2