android int to hex converting
Asked Answered
A

7

22

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?

Absenteeism answered 6/8, 2013 at 10:52 Comment(3)
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. I realize this is two years old, but a solid grasp of the underlying mechanisms is a valuable thing for programmers to have. :)Fictional
@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!Rage
@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.Fictional
S
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.

Seminal answered 6/8, 2013 at 10:56 Comment(1)
For the moment i've tryed and it seems to make it the right way!Absenteeism
H
5
String.format("#%06X", (0xFFFFFF & colorYellow));

Output: #FFC107

Hamer answered 17/11, 2017 at 11:47 Comment(0)
S
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

Subeditor answered 6/8, 2013 at 10:56 Comment(1)
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.Absenteeism
S
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);
Soloman answered 6/8, 2013 at 11:5 Comment(0)
D
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);
   }

}
Dulcimer answered 6/8, 2013 at 10:55 Comment(0)
P
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
Parachute answered 17/5, 2016 at 12:28 Comment(0)
D
0

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
Dardani answered 24/5 at 13:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.