java基本类型与byte数组之间相互转换、刚刚写的
パッケージcn.teey.utils;インポートjava.nio.charset.charset; public class byteutil {public static byte [] getBytes(short data){byte [] bytes = new byte [2];バイト[0] =(byte)(data&0xff);バイト[1] =(byte)((data&0xff00)>> 8);バイトを返します。 } public static byte [] getBytes(char data){byte [] bytes = new byte [2];バイト[0] =(byte)(data);バイト[1] =(byte)(data >> 8);バイトを返します。 } public static byte [] getBytes(int data){byte [] bytes = new byte [4];バイト[0] =(byte)(data&0xff);バイト[1] =(byte)((data&0xff00)>> 8);バイト[2] =(byte)((data&0xff0000)>> 16);バイト[3] =(byte)((data&0xff000000)>> 24);バイトを返します。 } public static byte [] getBytes(long data){byte [] bytes = new byte [8];バイト[0] =(byte)(data&0xff);バイト[1] =(byte)((data >> 8)&0xff);バイト[2] =(byte)((data >> 16)&0xff);バイト[3] =(byte)((data >> 24)&0xff);バイト[4] =(byte)((data >> 32)&0xff);バイト[5] =(byte)((data >> 40)&0xff);バイト[6] =(byte)((data >> 48)&0xff);バイト[7] =(byte)((data >> 56)&0xff);バイトを返します。 } public static byte [] getBytes(float data){int intbits = float.floattointbits(data); getBytes(intbits)を返します。 } public static byte [] getBytes(double data){long intbits = double.doubletolongbits(data); getBytes(intbits)を返します。 } public static byte [] getBytes(string data、string charsetname){charset charset = charset.forname(charsetname); data.getBytes(charset); } public static byte [] getBytes(string data){return getBytes(data、 "gbk"); } public static short getShort(byte [] bytes){return(short)((0xff&bytes [0])|(0xff00&(bytes [1] << 8))); } public static char getChar(byte [] bytes){return(char)((0xff&bytes [0])|(0xff00&(bytes [1] << 8))); } public static int getint(byte [] bytes){return(0xff&bytes [0])| (0xff00&(バイト[1] << 8))| (0xff0000&(バイト[2] << 16))| (0xff000000&(バイト[3] << 24)); } public static long getlong(byte [] bytes){return(0xffl&(long)bytes [0])| (0xff00l&((long)bytes [1] << 8))| (0xff0000l&((long)バイト[2] << 16))| (0xff000000l&((long)bytes [3] << 24))| (0xff00000000l&((long)bytes [4] << 32))| (0xff000000000000l&((long)bytes [5] << 40))| (0xff00000000000000l&((long)bytes [6] << 48))| (0xff0000000000000000l&((long)bytes [7] << 56)); } public static float getfloat(byte [] bytes){return float.intbitstofloat(getint(bytes)); } public static double getDouble(byte [] bytes){long l = getLong(bytes); System.out.println(l); double.longbitstodouble(l)を返します。 } public static string getString(byte [] bytes、string charsetname){return new string(bytes、charset.forname(charsetname)); } public static string getString(byte [] bytes){return getString(bytes、 "gbk"); } public static void main(string [] args){short s = 122; int i = 122; long l = 1222222; char c = 'a';フロートF = 122.22F;ダブルD = 122.22;文字列string = "我是好孩子"; 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(getCher(getBytes(c))); System.out.println(getFloat(getBytes(f))); System.out.println(getDouble(getBytes(d))); System.out.println(getString(getBytes(string))); }}以上这篇java