Java字節(jié)流與基本數(shù)據(jù)類型的轉(zhuǎn)換實(shí)例
在實(shí)際開(kāi)發(fā)中,我們經(jīng)常遇到與嵌入式進(jìn)行通信的情況,而由于一些嵌入式設(shè)備的處理能力較差,往往以二進(jìn)制的數(shù)據(jù)流的形式傳輸數(shù)據(jù),在此將這些常見(jiàn)的轉(zhuǎn)換做一總結(jié)。
注意:默認(rèn)傳輸時(shí)使用小端模式
將字節(jié)流轉(zhuǎn)換為int類型數(shù)據(jù)
public static int getInt(byte[] bytes) {
return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16))
| (0xff000000 & (bytes[3] << 24));
}
將字節(jié)流轉(zhuǎn)換為long類型數(shù)據(jù)
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)));
}
將字節(jié)流轉(zhuǎn)換為float類型數(shù)據(jù)
public static float getFloat(byte[] bytes){
int temp=getInt(bytes);
return Float.intBitsToFloat(temp);
}
將字節(jié)流轉(zhuǎn)換為double類型數(shù)據(jù)
public static double getDouble(byte[] bytes){
long temp=getLong(bytes);
return Double.longBitsToDouble(temp);
}
將int類型數(shù)據(jù)轉(zhuǎn)換為字節(jié)流
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;
}
將long類型數(shù)據(jù)轉(zhuǎn)換為字節(jié)流
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;
}
將float類型數(shù)據(jù)轉(zhuǎn)換為字節(jié)流
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;
}
將double類型數(shù)據(jù)轉(zhuǎn)換為字節(jié)流
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java+Eclipse+Selenium環(huán)境搭建的方法步驟
這篇文章主要介紹了Java+Eclipse+Selenium環(huán)境搭建的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
@Slf4j?如何實(shí)現(xiàn)日志輸入到外部文件
這篇文章主要介紹了@Slf4j?如何實(shí)現(xiàn)日志輸入到外部文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12
java eclipse 整個(gè)項(xiàng)目或包查找只定字符串并替換操作
這篇文章主要介紹了java eclipse 整個(gè)項(xiàng)目或包查找只定字符串并替換操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
Java8 使用流抽取List<T>集合中T的某個(gè)屬性操作
這篇文章主要介紹了Java8 使用流抽取List<T>集合中T的某個(gè)屬性操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02
Java設(shè)計(jì)模式之解釋器模式_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
解釋器模式是一個(gè)比較少用的模式,本人之前也沒(méi)有用過(guò)這個(gè)模式。下面我們就來(lái)一起看一下解釋器模式2017-08-08

