Recently in the C + + socket and the Java Socket Communication program, involving the transmission of integer floating point number, need to restore data from a byte array, check some information, summarized as follows
1. Machine representations of integers and floating-point numbers
Inside the machine, whether it is an integer or a floating-point number, it is stored in the form of a binary string.
The integer may be the original code, the complement indicates that the floating-point number has the order of the mantissa two parts, no matter what is a binary string, but how this binary string indicates that different machines may take different scenarios.
About floating-point numbers:
A binary string of 32-bit floating-point numbers on a machine may get different values when another machine interprets it as a 32-bit floating-point number, so that when the network is transmitted on different machines, it is probably only possible to use a string, but fortunately, theIEEE 754 specification The representation of floating-point numbers is standardized, and the 32-bit binary string of a floating-point number is the same as long as the standard is complied with, and the CPU-independent, language-independent
About integers:
The integer value of a single byte is a byte, an integer that occupies multiple bytes , and its representation is different, with the so-called small-end method, the big-endian method, which can be seen in the figure below.
2. Small-end method, big-endian method and host byte order, network byte order
Small-end method and big-endian method
The network byte order is the big endian, because all binary integers in the TCP/IP header are required in this order when they are transmitted over the network, so it is called the network byte order.
Host byte order refers to the byte order of the host relative to the byte order of the network transmission, there is the big-endian notation, the small-ended notation.
3. The conversion code between the basic data type in Java and the byte array byte[] is given below
The order of byte[] in this article is in the "big endian order", which means that for integers 0x11223344
Byte[0] Save 0x11,byte[1] Save 0x22,byte[2] Save 0x33,byte[3] Save 0x44
Using this "big endian", from byte[] to int, or int to byte[], the order of byte must be the "big-endian order" described above, so the advantage is that the server can call htonl,htons and other functions to convert byte order to network byte order, The point is that the server and the client have a consistent representation specification that seems to be more pleasing to the network byte order.
java specifies the length of a variety of basic types, so the length of the corresponding byte array is also fixed, independent of the machine.
The following is the code for the Char,short,int,float,long,double transformation, with the core being to take the bit operation and remove the 8 bits in a byte to convert.
To a large extent, the implementation in the http://tjmljw.iteye.com/blog/1767716 article is referenced ( This article uses a byte array of "small-endian order").
Import Java.nio.byteorder;public class Bytearrayconveter {//char converted to byte[2] array public static byte[] Getbytearray (char c) { Byte[] B = new Byte[2];b[0] = (byte) ((C & 0xff00) >> 8); b[1] = (byte) (C & 0X00FF); return b;} Gets a charpublic static char GetChar (byte[] arr, int index) {return (char) (0XFF00 & Arr[index] From the index of a byte array in contiguous two bytes << 8 | (0xFF & Arr[index + 1]));} Short converted to byte[2] array public static byte[] Getbytearray (short s) {byte[] b = new Byte[2];b[0] = (byte) ((S & 0xff00) ;> 8); b[1] = (byte) (S & 0x00ff); return b;} Obtains a shortpublic static short Getshort (byte[] arr, int index) {return (short) (0xff00 & Arr[ind) from a contiguous two bytes from the index of a byte array EX] << 8 | (0xFF & Arr[index + 1]));} int to byte[4] array public static byte[] Getbytearray (int i) {byte[] b = new Byte[4];b[0] = (byte) ((I & 0xff000000) B[1] = (byte) ((I & 0x00ff0000) >>;>) b[2] = (byte) ((I & 0x0000ff00) >> 8); b[3] = (byte) (I & 0X000000FF); rEturn b;} Gets a intpublic static int getInt (byte[] arr, int index) {return (0xff000000 & (Arr[index+0] <) from 4 consecutive bytes at index of the byte array ;< 24)) | (0x00ff0000 & (arr[index+1] << 16)) | (0x0000ff00 & (Arr[index+2] << 8)) | (0X000000FF & Arr[index+3]);} Float converted to byte[4] array public static byte[] Getbytearray (float f) {int intbits = float.floattointbits (f);// interprets the binary string in float as int integer return Getbytearray (intbits);} Gets a floatpublic static float getfloat (byte[] arr, int index) {return float.intbitstofloat () from the index of byte array of 4 consecutive bytes GetInt (arr, index));} Long to byte[8] array public static byte[] Getbytearray (long l) {byte b[] = new Byte[8];b[0] = (byte) (0xFF & (L >> ; B[1] = (byte) (0xFF & (L >>)), b[2] = (byte) (0xFF & (L >>)); b[3] = (byte) (0xFF & (l (>>)); B[4] = (byte) (0xFF & (L >>)); b[5] = (byte) (0xFF & (L >>)); B[6] = (byte) (0xf F & (L >> 8)); B[7] = (byte) (0xFF & L); return b; //8 consecutive bytes from index of byte array to obtain a longpublic static long Getlong (byte[] arr, int index) {return (0xff00000000000000l & (Lon g) arr[index+0] << 56)) | (0x00ff000000000000l & ((Long) arr[index+1] << 48)) | (0x0000ff0000000000l & ((Long) arr[index+2] << 40)) | (0x000000ff00000000l & ((Long) arr[index+3] << 32)) | (0x00000000ff000000l & ((Long) arr[index+4] << 24)) | (0x0000000000ff0000l & ((Long) arr[index+5] << 16)) | (0x000000000000ff00l & ((Long) arr[index+6] << 8)) | (0x00000000000000ffl & (Long) arr[index+7]);} Double converts to byte[8] array public static byte[] Getbytearray (double D) {Long longbits = double.doubletolongbits (d); return Getbytearray (longbits);} Gets a doublepublic static double getdouble (byte[] arr, int index) {return double.longbitstodouble from the index of a byte array with 8 consecutive bytes. (Getlong (arr, index));} public static void Main (string[] args) {System.out.println (Byteorder.nativeorder ()); if (Args.length < 1) { System.out.println ("Enter' Char ' test method about char '); System.out.println ("Enter ' short ' test method on short"); System.out.println ("Enter ' int ' test method about int"); System.out.println ("Enter ' float ' test method about float"); System.out.println ("Enter ' long ' test method about Long"); System.out.println ("Enter ' Double ' test method about Double"); return;} if (args[0].equals ("char")) {char c = ' \u0000 '; while (C < ' \uffff ') {System.out.println (GetChar (Getbytearray (c), 0)); C + +;}} else if (args[0].equals ("short")) {Short S = short.min_value;while (S < short.max_value) {System.out.println (getshort (Getbytearray (s), 0)); s++;}} else if (args[0].equals ("int")) {int i = Integer.min_value;while (i < Integer.max_value) {System.out.println (GetInt ( Getbytearray (i), 0)); i++;}} else if (args[0].equals ("float")) {float F = float.min_value;while (F < float.max_value) {System.out.println (getfloat (Getbytearray (f), 0)); f+=1.1111f;}} else if (args[0].equals ("Long")) {Long L = long.min_value;while (L < long.max_value) {System.out.println(Getlong (Getbytearray (L), 0)); l++;}} else if (args[0].equals ("double")) {double d = double.min_value;while (D < double.max_value) {System.out.println ( GetDouble (Getbytearray (d), 0));d +=1.111d;}}}
Description
This article was published by Giantpoplar in Csdn
Article Address http://write.blog.csdn.net/postedit/47657333
Reproduced please retain this note
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
byte array byte[] and integer, floating-point data conversion--java code