Java 基础 byte[]与各种数据类型互相转换的简单示例
Java基础byte[]与各种数据类型互相转换的简单示例
这里对byte[]类型对long,int,double,float,short,cahr,object,string类型相互转换的实例,
在socket开发过程中,通常需要将一些具体的值(这些值可能是各种Java类型)转化为byte[]类型,为此我总结了如下这个示例,贴出来,以便经常翻看:
publicclassTestCase{ /** *short到字节数组的转换. */ publicstaticbyte[]shortToByte(shortnumber){ inttemp=number; byte[]b=newbyte[2]; for(inti=0;i<b.length;i++){ b[i]=newInteger(temp&0xff).byteValue();//将最低位保存在最低位 temp=temp>>8;//向右移8位 } returnb; } /** *字节数组到short的转换. */ publicstaticshortbyteToShort(byte[]b){ shorts=0; shorts0=(short)(b[0]&0xff);//最低位 shorts1=(short)(b[1]&0xff); s1<<=8; s=(short)(s0|s1); returns; } /** *int到字节数组的转换. */ publicstaticbyte[]intToByte(intnumber){ inttemp=number; byte[]b=newbyte[4]; for(inti=0;i<b.length;i++){ b[i]=newInteger(temp&0xff).byteValue();//将最低位保存在最低位 temp=temp>>8;//向右移8位 } returnb; } /** *字节数组到int的转换. */ publicstaticintbyteToInt(byte[]b){ ints=0; ints0=b[0]&0xff;//最低位 ints1=b[1]&0xff; ints2=b[2]&0xff; ints3=b[3]&0xff; s3<<=24; s2<<=16; s1<<=8; s=s0|s1|s2|s3; returns; } /** *long类型转成byte数组 */ publicstaticbyte[]longToByte(longnumber){ longtemp=number; byte[]b=newbyte[8]; for(inti=0;i<b.length;i++){ b[i]=newLong(temp&0xff).byteValue();//将最低位保存在最低位temp=temp //>>8;//向右移8位 } returnb; } /** *字节数组到long的转换. */ publicstaticlongbyteToLong(byte[]b){ longs=0; longs0=b[0]&0xff;//最低位 longs1=b[1]&0xff; longs2=b[2]&0xff; longs3=b[3]&0xff; longs4=b[4]&0xff;//最低位 longs5=b[5]&0xff; longs6=b[6]&0xff; longs7=b[7]&0xff; //s0不变 s1<<=8; s2<<=16; s3<<=24; s4<<=8*4; s5<<=8*5; s6<<=8*6; s7<<=8*7; s=s0|s1|s2|s3|s4|s5|s6|s7; returns; } /** *double到字节数组的转换. */ publicstaticbyte[]doubleToByte(doublenum){ byte[]b=newbyte[8]; longl=Double.doubleToLongBits(num); for(inti=0;i<8;i++){ b[i]=newLong(l).byteValue(); l=l>>8; } returnb; } /** *字节数组到double的转换. */ publicstaticdoublegetDouble(byte[]b){ longm; m=b[0]; m&=0xff; m|=((long)b[1]<<8); m&=0xffff; m|=((long)b[2]<<16); m&=0xffffff; m|=((long)b[3]<<24); m&=0xffffffffl; m|=((long)b[4]<<32); m&=0xffffffffffl; m|=((long)b[5]<<40); m&=0xffffffffffffl; m|=((long)b[6]<<48); m&=0xffffffffffffffl; m|=((long)b[7]<<56); returnDouble.longBitsToDouble(m); } /** *float到字节数组的转换. */ publicstaticvoidfloatToByte(floatx){ //先用Float.floatToIntBits(f)转换成int } /** *字节数组到float的转换. */ publicstaticfloatgetFloat(byte[]b){ //4bytes intaccum=0; for(intshiftBy=0;shiftBy<4;shiftBy++){ accum|=(b[shiftBy]&0xff)<<shiftBy*8; } returnFloat.intBitsToFloat(accum); } /** *char到字节数组的转换. */ publicstaticbyte[]charToByte(charc){ byte[]b=newbyte[2]; b[0]=(byte)((c&0xFF00)>>8); b[1]=(byte)(c&0xFF); returnb; } /** *字节数组到char的转换. */ publicstaticcharbyteToChar(byte[]b){ charc=(char)(((b[0]&0xFF)<<8)|(b[1]&0xFF)); returnc; } /** *string到字节数组的转换. */ publicstaticbyte[]stringToByte(Stringstr)throwsUnsupportedEncodingException{ returnstr.getBytes("GBK"); } /** *字节数组到String的转换. */ publicstaticStringbytesToString(byte[]str){ Stringkeyword=null; try{ keyword=newString(str,"GBK"); }catch(UnsupportedEncodingExceptione){ e.printStackTrace(); } returnkeyword; } /** *object到字节数组的转换 */ @Test publicvoidtestObject2ByteArray()throwsIOException, ClassNotFoundException{ //Objectobj=""; Integer[]obj={1,3,4}; ////objecttobytearray ByteArrayOutputStreambo=newByteArrayOutputStream(); ObjectOutputStreamoo=newObjectOutputStream(bo); oo.writeObject(obj); byte[]bytes=bo.toByteArray(); bo.close(); oo.close(); System.out.println(Arrays.toString(bytes)); Integer[]intArr=(Integer[])testByteArray2Object(bytes); System.out.println(Arrays.asList(intArr)); byte[]b2=intToByte(123); System.out.println(Arrays.toString(b2)); inta=byteToInt(b2); System.out.println(a); } /** *字节数组到object的转换. */ privateObjecttestByteArray2Object(byte[]bytes)throwsIOException, ClassNotFoundException{ //byte[]bytes=null; Objectobj; //bytearraytoobject ByteArrayInputStreambi=newByteArrayInputStream(bytes); ObjectInputStreamoi=newObjectInputStream(bi); obj=oi.readObject(); bi.close(); oi.close(); System.out.println(obj); returnobj; } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!