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.
Package javaBasic.oi.byteoper; public class integeroper{public static void Main (string[] args) { System.out.println ("17 of 16 binary:" + Integer.tohexstring (+)); System.out.println ("octal in 17: " + integer.tooctalstring); SYSTEM.OUT.PRINTLN ("17 binary: " + integer.tobinarystring); System.out.println (Integer.valueof ("one")); System.out.println (integer.valueof ("8"); System.out.println (integer.valueof ("00010001", 2));} }
Right-click to run, we can see the following output in the console:
17 of 16 binary: 1117 octal: 2117 binary: 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
package javaBasic.oi.byteoper; public class integerconvert{/** * int to byte array */public static byte[] Int2bytes (int inta) {//32-bit int can be stored in byte array of length 4 byte[] bytes = new Byte[4]; for (int i = 0; i < bytes.length; i++) bytes[i] = (byte) (int) ((Inta >> i * 8) & 0xff);//Shift and zeroing return bytes; }/** * byte array to 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);//Shift and zeroing return INTA; } public static void Main (string[] args) {//Convert my study number to bytecode byte[] bytes = Integerconvert.int2bytes (1206010035); System.out.println (Bytes[0] + "" + bytes[1] + "+ bytes[2] +" "+ bytes[3]); Byte code can be converted back to System.out.println (Integerconvert.bytes2int (bytes)); } }
Run, right-click Run and you can see the following output:
-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:
public class longconvert{ /** * Long to byte array * /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);//Shift and clear 0 R Eturn bytes; } /** * Byte array goes 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);//Shift and clear 0 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:
Package javaBasic.oi.byteoper; public class stringconvert{public static void Main (string[] args) { String str = "Li Qiangqiang"; byte[] bytes = Str.getbytes (); Print byte array System.out.println ("' Li Qiangqiang ' byte array as:"); for (int i = 0; i < bytes.length; i++) System.out.print ("\ T" + bytes[i]);} }
by right-clicking Run, you can see the following output:
The byte array for 12 ' Li Qiangqiang ' is: -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