Java基本类型与byte数组之间相互转换方法
Java基本类型与byte数组之间相互转换,刚刚写的
packagecn.teaey.utils; importjava.nio.charset.Charset; publicclassByteUtil { publicstaticbyte[]getBytes(shortdata) { byte[]bytes=newbyte[2]; bytes[0]=(byte)(data&0xff); bytes[1]=(byte)((data&0xff00)>>8); returnbytes; } publicstaticbyte[]getBytes(chardata) { byte[]bytes=newbyte[2]; bytes[0]=(byte)(data); bytes[1]=(byte)(data>>8); returnbytes; } publicstaticbyte[]getBytes(intdata) { byte[]bytes=newbyte[4]; bytes[0]=(byte)(data&0xff); bytes[1]=(byte)((data&0xff00)>>8); bytes[2]=(byte)((data&0xff0000)>>16); bytes[3]=(byte)((data&0xff000000)>>24); returnbytes; } publicstaticbyte[]getBytes(longdata) { byte[]bytes=newbyte[8]; bytes[0]=(byte)(data&0xff); bytes[1]=(byte)((data>>8)&0xff); bytes[2]=(byte)((data>>16)&0xff); bytes[3]=(byte)((data>>24)&0xff); bytes[4]=(byte)((data>>32)&0xff); bytes[5]=(byte)((data>>40)&0xff); bytes[6]=(byte)((data>>48)&0xff); bytes[7]=(byte)((data>>56)&0xff); returnbytes; } publicstaticbyte[]getBytes(floatdata) { intintBits=Float.floatToIntBits(data); returngetBytes(intBits); } publicstaticbyte[]getBytes(doubledata) { longintBits=Double.doubleToLongBits(data); returngetBytes(intBits); } publicstaticbyte[]getBytes(Stringdata,StringcharsetName) { Charsetcharset=Charset.forName(charsetName); returndata.getBytes(charset); } publicstaticbyte[]getBytes(Stringdata) { returngetBytes(data,"GBK"); } publicstaticshortgetShort(byte[]bytes) { return(short)((0xff&bytes[0])|(0xff00&(bytes[1]<<8))); } publicstaticchargetChar(byte[]bytes) { return(char)((0xff&bytes[0])|(0xff00&(bytes[1]<<8))); } publicstaticintgetInt(byte[]bytes) { return(0xff&bytes[0])|(0xff00&(bytes[1]<<8))|(0xff0000&(bytes[2]<<16))|(0xff000000&(bytes[3]<<24)); } publicstaticlonggetLong(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)); } publicstaticfloatgetFloat(byte[]bytes) { returnFloat.intBitsToFloat(getInt(bytes)); } publicstaticdoublegetDouble(byte[]bytes) { longl=getLong(bytes); System.out.println(l); returnDouble.longBitsToDouble(l); } publicstaticStringgetString(byte[]bytes,StringcharsetName) { returnnewString(bytes,Charset.forName(charsetName)); } publicstaticStringgetString(byte[]bytes) { returngetString(bytes,"GBK"); } publicstaticvoidmain(String[]args) { shorts=122; inti=122; longl=1222222; charc='a'; floatf=122.22f; doubled=122.22; Stringstring="我是好孩子"; System.out.println(s); System.out.println(i); System.out.println(l); System.out.println(c); System.out.println(f); System.out.println(d); System.out.println(string); System.out.println("**************"); System.out.println(getShort(getBytes(s))); System.out.println(getInt(getBytes(i))); System.out.println(getLong(getBytes(l))); System.out.println(getChar(getBytes(c))); System.out.println(getFloat(getBytes(f))); System.out.println(getDouble(getBytes(d))); System.out.println(getString(getBytes(string))); } }
以上这篇Java基本类型与byte数组之间相互转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。