Want to convert Decimal from signed 2's complement to hexdecimal without signed 2's complement (ex : -73 -> B7)

383 Views Asked by At

i am receving sensor data which sends me Decimal from signed 2's complement want to convert it to hex decimal without signed 2's complement

1

There are 1 best solutions below

0
Stephen C On
String decimal = "-73";
int number = Integer.parseInt(decimal);
String unsignedHex = String.format("%02X", number & 0xff);

The parseInt call converts the signed decimal string to a (signed) int value.

The format call converts the int to an unsigned byte string in uppercase hexadecimal:

  • The number & 0xff expression strips off the sign extension.
  • The "%02X" format says uppercase hexadecimal (X) in a 2 character field. The 0 means zero padded. Read Format String Syntax for more information.