byte array byte[] and integer, floating-point data conversion--java code

Source: Internet
Author: User

The recent communication between C + + sockets and Java sockets involves the transmission of integer floating-point numbers. You need to restore the data from the byte array and look up some information. Summary for example the following

1. Machine representations of integers and floating-point numbers

Inside the machine. Whether it is an integer or a floating-point number. are stored in the form of a binary string.

The integer may be the original code. The complement indicates that the floating-point number consists of two parts, the mantissa of the Order code. Regardless of how it is a binary string. But how this binary string indicates that different machines might take different scenarios.

About floating-point numbers:

A binary string of 32-bit floating-point numbers on a machine can get different values when another machine interprets it as a 32-bit floating-point number, so when the network is transmitting on different machines, it's probably just a string-passing way. Just lucky. The IEEE 754 specification standardizes the representation of floating-point numbers. In different programming languages that conform to this standard, the 32-bit binary string of a floating-point number is the same, not CPU-independent, language-independent.

About integers:

The integer value of a single byte is one byte, and an integer that occupies multiple bytes has a different representation, with the so-called small-end method. The big-endian method, in detail, can see the following figure.

2. Small-end method, big-endian method and host byte order, network byte order

The small end method and the big-endian method see

The network byte order is the big endian sequence, because all the binary integers in the TCP/IP header are required in this order when they are transmitted in the network, so it is also called the network byte order.

Host byte order refers to the byte order on the host that is the byte order relative to the network transport. There are big-endian representations, small-end representations.

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". The meaning of this sentence is that for integers 0x11223344

Byte[0] Save 0x11. BYTE[1] Save 0x22. BYTE[2] Save 0x33,byte[3] Save 0x44

Use such "big-endian order", from byte[] to int, or int to byte[]. In this case, the order of byte must be the "big-endian order" described above, so the advantage is that the server can call Htonl when it is sent. The htons function converts byte order to network byte order, in fact the key is that the server and client have a consistent representation specification, which 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 conversion. The core is to take the bit operation, remove 8 bits in a byte, and convert.

To a large extent, the implementation of the http://tjmljw.iteye.com/blog/1767716 article ( which uses a byte array of the "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] &lt) 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 &GT;&GT ;  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


byte array byte[] and integer, floating-point data conversion--java code

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.