


Detailed explanation of conversion examples between Java byte streams and basic data types
This article mainly introduces examples of conversion between Java byte streams and basic data types. It has certain reference value. Interested friends can refer to it
In actual development, we often encounter the situation of communicating with embedded devices. Due to the poor processing capabilities of some embedded devices, data is often transmitted in the form of binary data streams. Here, these common Make a summary of the conversion.
Note: The default transmission mode is little endian
Convert the byte stream to int type data
public static int getInt(byte[] bytes) { return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24)); }
Convert the byte stream to long type data
public static long getLong(byte[] bytes) { return ((0xffL & (long) bytes[0]) | (0xff00L & ((long) bytes[1] << 8)) | (0xff0000L & ((long) bytes[2] << 16)) | (0xff000000L & ((long) bytes[3] << 24)) | (0xff00000000L & ((long) bytes[4] << 32)) | (0xff0000000000L & ((long) bytes[5] << 40)) | (0xff000000000000L & ((long) bytes[6] << 48)) | (0xff00000000000000L & ((long) bytes[7] << 56))); }
Convert the byte stream to float type data
public static float getFloat(byte[] bytes){ int temp=getInt(bytes); return Float.intBitsToFloat(temp); }
Convert the byte stream to double type data
public static double getDouble(byte[] bytes){ long temp=getLong(bytes); return Double.longBitsToDouble(temp); }
Convert the int type data to Byte stream
public static byte[] getByteFromInt(int data){ byte[] temp=new byte[4]; temp[0]=(byte)(0xFF&(data)); temp[1]=(byte)(0xFF&(data>>8)); temp[2]=(byte)(0xFF&(data>>16)); temp[3]=(byte)(0xFF&(data>>24)); return temp; }
Convert long type data to byte stream
public static byte[] getByteFromLong(long data){ byte[] temp=new byte[8]; temp[0]=(byte)(0xFF&(data)); temp[1]=(byte)(0xFF&(data>>8)); temp[2]=(byte)(0xFF&(data>>16)); temp[3]=(byte)(0xFF&(data>>24)); temp[4]=(byte)(0xFF&(data>>32)); temp[5]=(byte)(0xFF&(data>>40)); temp[6]=(byte)(0xFF&(data>>48)); temp[7]=(byte)(0xFF&(data>>56)); return temp; }
Convert float type data to byte stream
public static byte[] getByteFromFloat(float data){ byte[] temp=new byte[4]; int tempInt=Float.floatToIntBits(data); temp[0]=(byte)(0xFF&(tempInt)); temp[1]=(byte)(0xFF&(tempInt>>8)); temp[2]=(byte)(0xFF&(tempInt>>16)); temp[3]=(byte)(0xFF&(tempInt>>24)); return temp; }
Convert double type data to bytes Stream
public static byte[] getByteFromDouble(double data){ byte[] temp=new byte[8]; long tempLong=Double.doubleToLongBits(data); temp[0]=(byte)(0xFF&(tempLong)); temp[1]=(byte)(0xFF&(tempLong>>8)); temp[2]=(byte)(0xFF&(tempLong>>16)); temp[3]=(byte)(0xFF&(tempLong>>24)); temp[4]=(byte)(0xFF&(tempLong>>32)); temp[5]=(byte)(0xFF&(tempLong>>40)); temp[6]=(byte)(0xFF&(tempLong>>48)); temp[7]=(byte)(0xFF&(tempLong>>56)); return temp; }
The above is the detailed content of Detailed explanation of conversion examples between Java byte streams and basic data types. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft