Java 16 binary Conversion integer.tohexstring ()

Source: Internet
Author: User
Tags bitwise

In order to display the encoding of a byte-byte hexadecimal (two-bit hexadecimal representation), use:

integer.tohexstring ((Bytevar & 0x000000ff) | 0xffffff00). SUBSTRING (6)

The Bytevar & 0x000000ff function is that if the Bytevar is negative, the preceding 24 zeros are cleared, and the positive byte integer is unaffected.

(...) | The effect of 0xffffff00 is that if the Bytevar is positive, the first 24 bits are set to one, so that when the tohexstring outputs a hexadecimal of byte integers less than or equal to 15, the second-to-last digit is zero and is not discarded. This allows the last two bits to be intercepted by the substring method.

Example description

Importjunit.framework.TestCase; Public classHexextendsTestCase { Public voidTestpositiveinttohex () {//If the positive number is less than 15 o'clock, only one bit is entered, not the two-bit standard hexadecimal output we imagined, which solves the problem laterSystem.out.println (integer.tohexstring (2));//2System.out.println (integer.tohexstring (15));//FSystem.out.println (Integer.tohexstring (16));//TenSystem.out.println (integer.valueof ("F", 16));// -    }    /** INTEGER.VALUEOF () essentially calls the Integer.parseint () to complete, so * Integer.parseint () is the same as the integer.valueof () function, except that the return value is not * Just like that.*/     Public voidTestnegativeinttohex () {//negative integer, the previous input of the extra FF, not remove the front redundant FF, press and double-byte outputSystem.out.println (Integer.tohexstring ( -2). toUpperCase ());//Fffffffe//essentially 0xFF will be converted to 0x000000ff and then bit-opSystem.out.println (integer.tohexstring ( -2 & 0xFF). toUpperCase ());//FESystem.out.println (integer.tohexstring ( -2 & 0x000000ff). toUpperCase ());//FE//Note that the FE output will not be 2 because the FE is not considered negative at this time, and valueof will treat all the numbers as positiveSystem.out.println (integer.valueof ("FE", 16));//254//If you want to output-2, you can only output as followsSystem.out.println (Integer.valueof ("-2", 16));//-2//so to think of the FE as negative, we can only add the minus sign in front, but the output here is not-2,//instead, the integer.valueof ("FE", 16) is calculated first, and then the result is preceded by a negativeSystem.out.println (integer.valueof ("-fe", 16));//-254        /*so if you want to enter a negative number, we can only first find the absolute value of the original code hexadecimal, and then preceded by a minus sign, * For example, 128, the absolute value 128 is hexadecimal 80, and then preceded by the minus sign -80*/System.out.println (integer.valueof ("-80", 16));//-128        /*Why do you think valueof all the numbers strings as positive? Take a look at the following three lines of code, because the maximum positive number is 2147483647, * if you add one on the 7fffffff basis, the operation will definitely make an error (this is not the same as the direct output 0x80000000), * then you can prove 
    */System.out.println (integer.valueof ("7fffffff", 16));//2147483647//This sentence runs with an error because the maximum positive number is 7FFFFFFF, but 80000000 can be run because the integer range is not exceeded//System.out.println (integer.valueof ("80000000", +));//cannot run, has been injectedSystem.out.println (Integer.valueof ("-80000000", 16));//-2147483648        /*Note, the output is not negative, but positive, because the 0xFE is only 8 bits, and the integer is 32 bits, so in the form of an int will automatically fill 24 zeros, the first bit is zero, so the last is a positive number*/System.out.println (0xFE);//254System.out.println ( -0XFE);//-254//but the 0x80000000 is full, no need to fill, the first bit is one, so the last is negativeSystem.out.println (0x80000000);//-2147483648    }     Public voidTestnegativeinttobin () {System.out.println (integer.tobinarystring (-2));//11111111111111111111111111111110//essentially 0xFF will be converted to 0x000000ff and then bit-opSystem.out.println (integer.tobinarystring ( -2 & 0xFF));//11111110System.out.println (integer.tobinarystring ( -2 & 0x000000ff));//11111110//It's the same as the hex above.System.out.println (integer.valueof ("1111111111111111111111111111111", 2));//2147483647//The following statement runs with an error and has been injected//System.out.println (integer.valueof ("10000000000000000000000000000000", 2));System.out.println (Integer.valueof ("-10000000000000000000000000000000", 2));//-2147483648System.out.println (integer.valueof ("11111110", 2));//254System.out.println (Integer.valueof ("-11111110", 2));//-254        /*Note that in Java there is no direct use of binary representation of a number (currently only eight and hexadecimal direct notation), the following is actually a * octal number and decimal number*/System.out.println (010);//8SYSTEM.OUT.PRINTLN (10);//Ten    }     Public voidTestbytetohex () {byteNegativebyte =-2; bytePositivebyte = 2; /*The tohexstring method is of type int, so the pre-hex parameter is promoted to an integer and then converted, the process is as follows: * 10000010 (original code)->11111110 (complement)->11111111 11111111 11111 111 11111110 (LIFT) *->fffffffe (turn hex input output)*/System.out.println (integer.tohexstring (negativebyte). toUpperCase ());//Fffffffe        /*The first step 2 turn to Integer: * 10000010 (original code)->11111110 (complement)->11111111 11111111 11111111 11111110 (rotary type) * The second step to 0xFF Pre-fill 24 0: * 00000000 00000000 00000000 11111111 * Step three: Perform the 12th step with the bitwise operation: * 00000000 00000000 00000000 11 111110 * Last step: Turn hex result to FE*/System.out.println (integer.tohexstring (Negativebyte& 0xFF). toUpperCase ());//FE//another type of conversion that can be used for both negative and positive byte can be output with a full single byteSystem.out.println (integer.tohexstring (Negativebyte & 0x000000ff) | 0xffffff00). substring (6). toUpperCase ());//FESystem.out.println (integer.tohexstring (Positivebyte & 0x000000ff) | 0xffffff00). substring (6). toUpperCase ());// Geneva    }    /*** Bitwise arithmetic is the same as the type promotion mechanism in arithmetic operation .*/     Public voidtestbitemathematical () {System.out.println (0X8000000000000000L);//-9223372036854775808System.out.println ((int) 0x8000000000000000l);//0System.out.println (0x8000000000000010l);//-9223372036854775792System.out.println (0x80000000);//-2147483648System.out.println (0x80000010);//-2147483632//0x00000010 increases the growth integral type, the final result is the long integer type 0x8000000000000010lSystem.out.println (0x00000010 | 0x8000000000000000l);//-9223372036854775792//The 0x0010 is promoted into plastic, and the final result is the integral type 0x80000010System.out.println (0x0010 | 0x80000000);//-2147483632    }}

Java 16 binary Conversion integer.tohexstring ()

Related Article

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.