"Go" Java 16-in-conversion integer.tohexstring () __java

Source: Internet
Author: User
Tags bitwise number strings

Java 16 binary conversion integer.tohexstring () keyword: integer.tohexstring

To display the encoding for a byte-byte hexadecimal (two-bit hexadecimal representation), use:

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

The effect of Bytevar & 0X000000FF is that if Bytevar is a negative number, the preceding 24 zeros are cleared, and positive byte integers are unaffected. (...) | The 0xffffff00 effect is that if the Bytevar is a positive number, the first 24 digits are one, so that the tohexstring outputs a hexadecimal with a byte less than or equal to 15, and the penultimate digit is zero and is not discarded. This can be done through the substring method to intercept the last two digits.

  Java code import junit.framework.TestCase; public class Hex extends TestCase {public void Testpositiveinttohex () {//If the positive number is less than 15 o'clock, enter only one bit instead of the two-bit standard hexadecimal output we imagined, Solve this problem later System.out.println (integer.tohexstring (2));//2 System.out.println (integer.tohexstring);//f System.out.println (integer.tohexstring)//10 System.out.println (integer.valueof ("F", 16))//16}/* Integer.valueof () essentially calls Integer.parseint (), so * Integer.parseint () is the same as the integer.valueof () function, except that the return value is not * * * * * public void Testnegativeinttohex () {//negative integer, the previous input of the excess FF, did not remove the front redundant FF, in the form of double byte output System.out.println (integer.tohexstring ( -2). toUpperCase ());//fffffffe//essentially 0xFF will be converted into 0X000000FF after the bitwise Operation SYSTEM.OUT.PRINTLN (Integer.tohexstring ( -2 & 0xFF). toUpperCase ());//fe System.out.println (integer.tohexstring ( -2 & 0x000000ff). toUpperCase ());//fe//Note, The FE output will not be-2 because FE is not considered negative at this time, and valueof will consider all numeric strings as positive System.out.println (integer.valueof ("FE", 16);//254//If you want to output 2, System.out.println (Integer.valueof ("2", 16) can only be exported in the following form;//-2//So to see the FEIn negative terms, you can only add a minus sign to the front, but the output here is not 2,//instead of the integer.valueof ("FE", 16) before the result plus a negative System.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 add a minus sign, * For example, to 128, the absolute value of 128 for hexadecimal 80, and then precede with minus-80 * * System.out.println (integer.valueof ("80", 16))//-128/* Why say valueof all number strings as positive. See the following three lines of code, because the maximum positive number is 2147483647, * if you add one on the 7fffffff basis, the operation will certainly be wrong (this is not the same as the direct output 0x80000000), * then you can prove * * * SYSTEM.OUT.PRINTLN ( Integer.valueof ("7fffffff", 16);//2147483647//This sentence is run with an error, because the maximum positive number is 7fffffff, but it can run as-80000000, because it does not exceed the integer range// System.out.println (integer.valueof ("80000000", 16));/cannot run, has been injected System.out.println (integer.valueof ("80000000", 16 )//-2147483648/* Note, the output is not negative, but positive, because the 0xFE is only 8 digits, and the integer is 32 bits, so in the form of int before the * face will automatically fill 24 zeros, the first is zero, so the last is a positive number * * SYSTEM.OUT.PRINTLN ( 0xFE);//254 System.out.println ( -0XFE);//-254//But 0x80000000 full, no need to fill, first one, so the last negative System.out.println (0x80000000);//- 2147483648} public void Testnegativeinttobin () {System.out.println (integer.tobinarystring (-2)); 11111111111111111111111111111110//Essentially 0xFF will be like a turnReplace with 0x000000ff System.out.println (integer.tobinarystring ( -2 & 0xFF));//11111110 System.out.println ( Integer.tobinarystring ( -2 & 0x000000ff));//11111110//is the same as the hexadecimal above System.out.println (integer.valueof (" 1111111111111111111111111111111 ", 2);//2147483647//The following statement runs an error and has been injected//system.out.println (integer.valueof (" 10000000000000000000000000000000 ", 2)); System.out.println (integer.valueof (" -10000000000000000000000000000000", 2));//-2147483648 System.out.println ( Integer.valueof ("11111110", 2));//254 System.out.println (Integer.valueof ("11111110", 2));//-254/* Note, Java does not directly use binary to represent a number (currently only supports octal and hexadecimal direct notation), the following is actually a * octal number and decimal number/SYSTEM.OUT.PRINTLN (010);//8 System.out.println (10); /10} public void Testbytetohex () {byte negativebyte =-2; byte positivebyte = 2;/* Tohexstring method type is int, so the parameter before the turn to Hex is promoted to integral type After the conversion, the process is as follows: * 10000010 (original code)->11111110 (complement)->11111111 11111111 11111111 11111110 (lifting) *->fffffffe (turn hex into output) * * System.out.println (Integer.tohexstring (negativebyte). toUpperCase ());//FFFFFFFE/* First step 2 to the whole type: * 10000010 (original code)->11111110 (complement)->11111111 11111111 11111111 11111110 (Round) * The second step of 0xFF 24 0: * 000 00000 00000000 00000000 11111111 * Third step: The result of the 12th and bitwise operation: * 00000000 00000000 00000000 11111110 * Last step: Turn hexadecimal result to FE/SYSTEM.O Ut.println (integer.tohexstring (Negativebyte & 0xFF). toUpperCase ())//FE//Another conversion, can be for negative and positive byte can be in full single-byte output System.out.println (integer.tohexstring (Negativebyte & 0x000000ff) 0xffffff00). SUBSTRING (6). toUpperCase ()); /fe System.out.println (integer.tohexstring (Positivebyte & 0x000000ff) | 0xffffff00). SUBSTRING (6). toUpperCase () );//02}/** * bit operations are the same as the type promotion mechanism in arithmetic operations. * * public void testbitemathematical () {System.out.println (0x8000000000000000l);//- 9223372036854775808 System.out.println ((int) 0x8000000000000000l);//0 System.out.println (0x8000000000000010l);//- 9223372036854775792 System.out.println (0x80000000);//-2147483648 System.out.println (0x80000010);//-2147483632// 0X00000010 promotes the growth integral type, the final result is the long integral type 0x8000000000000010l System.out.println (0x00000010 | 0x8000000000000000l);//-9223372036854775792//0x0010 promoted into plastic surgery, the final result was integral type 0x80000010 System.out.println (0x0010 | 0x80000000 );//-2147483632}}  

 

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.