Pregunta

i have hex value 03E7 which is string in type.i need to apply 2s compliment in this string.and the resultant hex value should be in string format.i first converted it to binary then converted .is there is any simple method?

¿Fue útil?

Solución

Negative numbers are the 2's complement of positive numbers and vice versa, so I suppose you could parse your string into an int, multiply by -1, and then parse it back into the resulting hex string.

int intVal = Integer.parseInt("03E7", 16);
String twosComplement = Integer.toHexString((-1 * intVal));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top