Li Qiangqiang
on the basis of the previous article, Mason said the next Java I/o: bit operation bitwise Operation . In this story, the Masons take you into the Java in-depth explanation.
First, the Primer
In the Java world, 99% of all jobs are handled at this high level. So where are the binary, bytecode?
Self-answer: When cross-platform , it is the magic of the martial. For example, file reading and writing , data communication , and Java compiled bytecode files . Here is an example of data communication.
Java implements the Serializablle interface to an object, and it can be transformed into a series of bytes , while in communication it is not necessary to relate the data in different machine representations and byte order. Here masons to Serializablle interface, do not do a detailed explanation, after a separate explanation.
Second, Java Binary conversion
First recognize the data types in Java:
1, int integer: Byte (8-bit, -128~127), short (16-bit), int (32-bit), long (64-bit)
2, Float type: float (32-bit), double (64-bit)
2, char character: Unicode character (16 bit)
In other words, an int is equivalent to a byte array of length 4.
How does the binary in Java convert?
In Java, a series of action methods are provided in the class int shaping and char character type wrapper . For example, in Java.lang.Integer , theAPI:
Below the Masons write a demo, verify under.
?
| 123456789101112131415 |
package javaBasic.oi.byteoper;public class IntegerOper{ public static void main(String[] args) { System.out.println("17的十六进制: " + Integer.toHexString(17)); System.out.println("17的八进制: " + Integer.toOctalString(17)); System.out.println("17的二进制: " + Integer.toBinaryString(17)); System.out.println(Integer.valueOf("11", 16)); System.out.println(Integer.valueOf("21", 8)); System.out.println(Integer.valueOf("00010001", 2)); }} |
Right-click to run, we can see the following output in the console:
?
| 123456 |
17的十六进制: 1117的八进制: 2117的二进制: 10001171717 |
Add: If the value is too large, you need to call the method provided by Java.lang.Long.
Third, Java basic types and byte magic conversion
Here Mason thought of himself as a student, a typical OO thought. The study number: 1206010035 is the integer type, how to turn into a byte, the above said that the object has a byte code can communicate. So, the school about the number of this is the way the communication. Therefore, you will have to turn the study number into a byte code.
The Masons wrote a tool class Integerconvert.java:
?
| 123456789101112131415161718192021222324252627282930313233343536373839 |
package javaBasic.oi.byteoper;public class IntegerConvert{ /** * Int转字节数组 */ public static byte[] int2Bytes(int inta) { // 32位Int可存于长度为4的字节数组 byte[] bytes = new byte[4]; for (int i = 0; i < bytes.length; i++) bytes[i] = (byte)(int)((inta >> i * 8) & 0xff);// 移位和清零 return bytes; } /** * 字节数组转Int */ public static int bytes2Int(byte[] bytes) { int inta = 0; for (int i = 0; i < bytes.length; i++) inta += (int)((bytes[i] & 0xff) << i * 8);// 移位和清零 return inta; } public static void main(String[] args) { // 将我的学号转换成字节码 byte[] bytes = IntegerConvert.int2Bytes(1206010035); System.out.println(bytes[0] + " " + bytes[1] + " " + bytes[2] + " " + bytes[3]); // 字节码就可以转换回学号 System.out.println(IntegerConvert.bytes2Int(bytes)); }} |
Run, right-click Run and you can see the following output:
?
| 12 |
-77 64 -30 711206010035 |
The code is explained in detail below:
1. (Inta >> I * 8) & 0xFF
shift zeroing from left to right, press 8 bits to get 1 bytes .
2, the use of small-end method . The status byte is placed on the low-memory address side, which is the starting address of the value. Supplement: 32-bit medium-endian mode (PPC) and small segment End mode (x86).
Naturally, long also has its conversion method, as follows:
?
| 123456789101112131415161718192021222324252627 |
public class LongConvert{ /** * long 转 byte数组 */ public static byte[] long2Bytes(long longa) { byte[] bytes = new byte[8]; for (int i = 0; i < bytes.length; i++) bytes[i] = (byte)(long)(((longa) >> i * 8) & 0xff); // 移位和清零 return bytes; } /** * byte数组 转 long */ public static long bytes2Long(byte[] bytes) { long longa = 0; for (int i = 0; i < bytes.length; i++) longa += (long)((bytes[i] & 0xff) << i * 8); // 移位和清零 return longa; } } |
What about the string, the character array? Like Mason's name: Li Qiangqiang.
Java also provides a series of methods, in fact java.lang.String encapsulated char[], which is essentially the operation of the char array . The code is as follows:
?
| 1234567891011121314 |
package javaBasic.oi.byteoper;public class StringConvert{ public static void main(String[] args) { String str = "李强强"; byte[] bytes = str.getBytes(); // 打印字节数组 System.out.println("‘李强强‘的字节数组为:"); for (int i = 0; i < bytes.length; i++) System.out.print("\t" + bytes[i]); }} |
By right-clicking Run, you can see the following output:
?
| 12 |
‘李强强‘的字节数组为: -64 -18 -57 -65 -57 -65 |
Argument : Here we demonstrate a Chinese, which requires two bytes , which means that a Chinese is 16 bits .
Iv. talking about the data in Java communication
Here is a simple continuation of the story of Masons ' students.
, a student object in the library table has a property number. At this point the client sends this object to the server. The process is as follows:
1, the object implements the serializable interface.
Objects that implement the serializable interface can be converted to a series of bytes, and can be completely restored back to their original appearance at a later time.
2, its study number attribute value 1206010035, by the client to the byte code.
3. Byte-code transmission to the server
4. The server receives and converts to the object property value.
V. Summary
This article is a little bit more, Masons want to put this piece of knowledge to string up, and then slowly each explanation. Summarized as follows:
1. What is the binary conversion in Java?
2, the role of binary conversion in Java?
Writer:bysocket (mud and brick pulp carpenter)
Micro-Blog: Bysocket
Watercress: Bysocket
FaceBook: Bysocket
Twitter: Bysocket
Java I/O: An in-depth explanation of Java