Java programming those things 7-the concept of hexadecimal Author: Chen yuefeng from: http://blog.csdn.net/mailbomb

**1.2 hexadecimal Concept**Because it is impossible to create a symbol for each value, a composite value needs to be combined with a basic number. In this way, the concept of hexadecimal is introduced. In fact, all inputs are artificial creations, which are used for counting convenience. Currently, the most common hexadecimal format is decimal. Of course, other hexadecimal formats are also in use. For example, the idiom "half a pound" reflects the concept that one pound equals sixteen two in ancient times, that is, the hexadecimal counting method. In computer programming, binary, octal, decimal, and hexadecimal are commonly used. decimal is the most important expression. In programming, the default value written by everyone is decimal. There are two basic concepts for hexadecimal: base number and calculation rule. L base number refers to a basic number in a base system, that is, a number that cannot be split. For example, decimal is 0-9, binary is 0 and 1, octal is 0-7, hexadecimal is 0-9, A-F (both case and case ). Or you can simply remember it like this. If it is in the n-base, the base number is the number of [0, n-1]. The base number is the same as the base value and the base value is the same. Decimal has ten bases, and so on. L operation rules the operation rules are carry or borrow rules, which are similar to the concept of median weights in general computer books. For example, for decimal systems, the rule is "Full into one, borrow one to ten ", that is, the number at the low level is ten, and the number from the High Level is one, which is equivalent to the ten at the low level. The same is true for other binary systems. For binary systems, it is "Full binary into one, borrow one as two", and the same is true for octal and hexadecimal systems. In mathematics, numbers are represented in the following format: [numeric] hexadecimal number. For example, [10] 2 indicates the binary value 10.

**1.2.1 binary**Binary is the representation of computer internal data, so learning computer programming must be familiar with binary. Familiar with binary has the following purposes: l it is easier to understand the data storage methods of computers. Many conversions within a computer, such as strong conversion between data types, you can use binary to explain the final result value. L The speed of binary operations is higher. The speed of binary operations is much higher than that of decimal. For example, to calculate the N power of 2, the efficiency achieved by shift is more efficient than the mathematical method. L use binary values for data storage to store values in binary format. One is resource-saving, and information can be stored using binary bits, such as common hardware control information, are provided in binary format. As mentioned above, the binary system contains two bases: 0 and 1. The calculation rule is "Full binary into one, borrow one as two". The following describes the binary counting method briefly. For example, if 0-9 in decimal format is expressed in binary format, the values are 0, 1, 10, 11, 100,101,110,111,100, and respectively. Note: The values are separated by commas. The following are some basic calculation results of binary: l addition operation 0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10l subtraction 0-0 = 00-1 =-1 1-0 = 11-1 = 0 l multiplication 0 × 0 = 00 × 1 = 01 × 0 = 01 × 1 = 1l Division 0/0 meaningless 0/1 = 01/0 meaningless 1/1 = 1 the following are some suitable expressions: 110 + 111 = 1101 these basic computing structures are generally not directly used in actual development, but they can be used to deepen understanding of the binary concept.

**1.2.2 conversion between binary and decimal**Because the data in the computer is expressed in binary, And the decimal is the most commonly used in daily life, conversion is often required between them. The following describes the conversion method.

**1.2.2.1 convert decimal to binary**There are three methods to convert a decimal integer to a binary value: Division 2 remainder, Calculator conversion, and empirical method. Finally, we will give a brief introduction to the decimal conversion method. 1. The Division Two remainder method the Division Two remainder method is the most basic method during conversion and the most common method. The rule is as follows: Use decimal and 2 to remove the quotient and remainder obtained each time. The Merchant continues to divide with 2 until the quotient is zero and the remainder obtained for the first time is used as the binary low position, the remainder obtained at the last time is used as the high position of the binary. The number composed of the remainder is the binary value after conversion. For example, to convert a decimal value of 13 to a binary value, perform the following steps: the remainder is 13/2 = 6 16/2 = 3 03/2 = 1 11/2 = 0 1, and the final result is 1101. 2. The calculator in the Windows operating system can also be easily converted between hexadecimal values. Open the calculator in the accessories sub-menu in the program menu, select "scientific" from the View menu of the open calculator, and enter the decimal number you want to convert, for example, 13, then, the "binary" on the west side of the box is displayed, and the converted value is directly displayed in the calculator. 3. after the empirical method is familiar with binary, some basic mathematical transformations can be used to calculate the digits corresponding to decimal. before using the empirical method, note that the decimal values corresponding to the power 0-10 of 2 are: 1, 2, 4, 8, 16, 32, 64,128,256,512,102 4, which can greatly improve the conversion speed when converting some special numbers, for example, the number 65 can be converted as follows: 65 = 64 + 164 the binary format is 10000001. The binary format is 1. the binary format of 65 is 1000001. This is only suitable for converting some special numbers, adaptability is not widely used except the two-step remainder method. The general method used for decimal conversion is to take an integer by two. The rule is: first take decimal part by two and then obtain the integer part of the calculation result, then, multiply the decimal part in the result by two again until the decimal part is zero. Then, the first obtained integer part is taken as the high position of the binary decimal part, the subsequent integer part is the binary decimal point obtained after conversion. It should be noted that some decimal places cannot be accurately expressed in binary, so the conversion must be accurate, which is also the reason why the floating point calculation of the computer is not accurate. For example, to convert 0.25 to binary decimal places, perform the following steps: integer part 0.25 × 2 = 0.5 0 0.5 × 2 = 1.0 1 then 0.25 is converted to binary decimal places 0.01 if a decimal number has an integer part, if there are decimals, convert them separately.

**1.2.2.2 binary conversion to decimal**The method used to convert binary data into decimal data is the addition of the number multiplication right. The following uses decimal as an example to describe this method. For example, if the decimal number is 345, the bitwise of 5 is 100, and the bitwise of 3 is, the following expression is true: 345 = 5x1 + 4x10 + 3x100, which is the principle of the number multiplication right addition method. In fact, there are rules for bitwise weights of decimal integers. The bitwise right from the right to the left to the nth digit is ten (n-1) sides. For example, the bitwise is 10 (1-1 ), 10 is 10 (2-1), and so on. The bitwise weights of the binary integers are the same as those of the bitwise weights of the nth bitwise from the right to the left. For example, the expression for converting a binary integer 1011 to a decimal number is: [1011] 2 = 1x20 + 1x21 + 0x22 + 1x23 = 1 + 2 + 0 + 8 = 11 while for decimal places, the same method is also used, except that the bitwise permission rules of binary decimal places are: the first decimal places after the decimal point are 2-1 to the power, and the second place is 2-2 to the power, and so on. For example, the expression for converting a binary decimal number 0.1101 to a decimal number is [0.1101] 2 = 1 × 2-1 + 1 × 2-2 + 0 × 2-3 + 1 × 2-4 = 0.5 + 0.25 + 0 + 0.0625 = 0.8125 Similarly, if the binary contains integers and decimals, convert them separately.

**1.2.3 conversion between binary, octal, and hexadecimal**Although binary is a data expression in a computer, the binary base is too small, resulting in a long number. to simplify the writing of numbers, octal and hexadecimal are created. Octal and hexadecimal are simplified to binary, so the conversion from binary to octal is very simple. The method for converting a binary integer to an octal integer is "three digits", that is, starting from the right side, each three digits are converted to one octal digit, and so on, because the three digits in binary format can express a range of 111-, which is exactly the same as 0-7. For example, if the binary 10111 is converted to octal: the last three digits 111 are converted to 7, and the first digit 10 is converted to 2, The octal digit after conversion is 27. The method for converting a binary integer to a hexadecimal value is "four digits in one". For example, if 10111 is converted to a hexadecimal value, 0111 is converted to 7, 1 is converted to 1, the hexadecimal number after conversion is 17. The method for converting binary decimal points to octal decimal points is also "three digits", but the conversion starts from the decimal point, that is, the left side of the decimal point. For example, if 0.10111 is converted to octal 101 and 5,110 is converted to 6, the decimal number of octal is 0.56. Note that if the number of decimal places is less than three, it must be converted after zeros are added. The method for converting binary decimals to hexadecimal is also "four digits in one", but the conversion starts from the decimal High. For example, if the binary decimal point 0.10111 is converted to a hexadecimal decimal point, the 1011 is converted to B, and the 1000 is converted to 8, the hexadecimal value obtained after the conversion is 0. B8. If the binary number contains integers and decimals, the conversion is performed separately.