Java Converting Int to Hex and Back Again

Java converting int to hex and back again

It overflows, because the number is negative.

Try this and it will work:

int n = (int) Long.parseLong("ffff8000", 16);

Java Convert integer to hex integer

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.

Convert Integer to Hex in Java

The ˜ isn't a tilde, it is character with unicode 732.

When you convert it to a byte[], you get two bytes if you use UTF-8, -53 and -100

However if you ignore the second one and convert it to a char you get 152 (which is -53 & 0xFF)

You have a number of lossy transformations which makes it impossible to reverse them.

What you can do is convert the character to hexadecimal and back again.

String input = "˜";
String hex = DatatypeConverter.printHexBinary(input.getBytes("UTF-16BE"));
System.out.println("hex: "+hex);
String input2 = new String(DatatypeConverter.parseHexBinary(hex), "UTF-16BE");
System.out.println("input2: "+input2);

prints

hex: 02DC
input2: ˜

This will work for arbitrary Strings (of less than half a billion characters)

Convert int to hex string

It would be great if you can provide solution for general case, not
this specific case

This is a quick solution that will add a left 0 in the case the hex string length is not a multiple of 2.

StringBuilder sb = new StringBuilder();
sb.append(Integer.toHexString(myInt));
if (sb.length() % 2 > 0) {
sb.insert(0, '0'); // pad with leading zero if needed
}
String hex = sb.toString();

Convert Integer to Hex where decimal 1 representation is FFFFFF

I ended up solving this problem as follows:

public class Main {
public static String toHex(int d) {
if(d == 0) {
return "0";
}

String codes = "0123456789abcdef";

StringBuilder builder = new StringBuilder(8);
builder.setLength(8);

for (int i = 7; i >= 0; i--) {
/**
* 19 in binary is 10011 and 15 is 01111, when using & (and) bitwise operator
* It becomes 00011, which is 3
*/
builder.setCharAt(i, codes.charAt(d & 15));
/**
* Right arithmetic shift, for example:
* 19 in binary is 10011 after shifting to the right 4 places it becomes
* 00001
*/
d >>= 4;
}

return removeLeadingZeros(builder.toString());
}

public static String removeLeadingZeros(String str){
StringBuilder strB = new StringBuilder(str);
int index = 0;

while (strB.length() > 0 && strB.charAt(index) == '0') {
strB.deleteCharAt(index);
}

return strB.toString();
}

public static void main(String[] args) {
System.out.println(toHex(-1).equals(Integer.toHexString(-1))); // true
System.out.println(toHex(19).equals(Integer.toHexString(19))); // true
}
}

I want to say thanks to @rzwitserloot and @trincot for pointing me in the right direction.

The hex code can be found at eddmann, I added some comments and the ability to remove leading zeroes.

Hopefully, this helps others with a similar types of questions.

More about Arithmetic Shift and Bitwise Operations

Integer to two digits hex in Java

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

If you use X instead of x as suggested by aristar, then you don't need to use .toUpperCase().

Java negative int to hex and back fails

It's documented that Integer.toHexString returns a string representation of the integer as an unsigned value - while Integer.parseInt takes a signed int. If you use Integer.toString(value, 16) instead you'll get what you want.



Related Topics



Leave a reply



Submit