When byte is converted to 16, the reason why integer.tohexstring needs to be &ff

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.