See the following code:
String mac_no_ff = ""; for (int i = 0; i < 6; i++) { + = integer.tohexstring (Card[i] & 0xFF). toUpperCase (); + = integer.tohexstring (card[i]). toUpperCase (); } System.out.println (MAC); System.out.println (MAC_NO_FF);
This is the output .286ED488C8C3
286EFFFFFFD4FFFFFF88FFFFFFC8FFFFFFC3 28 normal conversion, 6E after the conversion of a series of FF this is because the receive parameter of Integer.tohexstring () is an int, not a byte, so the operation is considered to be the first to cast a byte to an int because the cast in Java is the same as the persisted value, The number in the computer is in the complement, the Java int is 32 bit 4 byte, the positive complement is the positive number itself, so there is no problem, strong to 32 bit when the preceding 24 bits will fill 0, and the complement of negative numbers is "the corresponding positive binary representation of all the bit negation (including the sign bit, 0 change 1, 1 Change 0) plus 1 ", so 32 bits 0x00 00 00 80 (0000 ... 0000 1000 0000) complement is 0xFF FF FF 80 (1111 ... 1111 1000 0000), the front is the padding of 1 so integer.tohexstring () will become the front of a bunch of f so to get the correct results, need to use Integer.tohexstring (Card[i] & 0xff), This will only take the last 8 bits (1byte=8 bit binary), the front is set 0, so the conversion is correct.
When byte is converted to 16, the reason why integer.tohexstring needs to be &ff