java基本類型與字節數組之間相互轉換,剛剛寫的
軟件包cn.teaey.utils;導入java.nio.charset.charset; public class byteutil {public static byte [] getBytes(簡短數據){byte [byte [bytes bytes = new byte [2];字節[0] =(byte)(data&0xff);字節[1] =(byte)((數據&0xff00)>> 8);返回字節; } public static byte [] getBytes(char data){byte [] bytes = new byte [2];字節[0] =(byte)(data);字節[1] =(字節)(數據>> 8);返回字節; } public static byte [] getBytes(int data){byte [] bytes = new byte [4];字節[0] =(byte)(data&0xff);字節[1] =(byte)((數據&0xff00)>> 8);字節[2] =(字節)((數據&0xff0000)>> 16);字節[3] =(字節)((數據&0xff000000)>> 24);返回字節; } public static byte [] getBytes(長數據){byte [] bytes = new byte [8];字節[0] =(byte)(data&0xff);字節[1] =(字節)((數據>> 8)&0xff);字節[2] =(字節)((數據>> 16)&0xff);字節[3] =(字節)((數據>> 24)&0xff);字節[4] =(byte)((數據>> 32)&0xff);字節[5] =(字節)((數據>> 40)&0xff);字節[6] =(字節)((數據>> 48)&0xff);字節[7] =(字節)((數據>> 56)&0xff);返回字節; } public static byte [] getBytes(float data){int intbits = float.floattointbits(data);返回getBytes(intbits); } public static byte [] getBytes(double data){long intbits = double.doubletolongongbits(data);返回getBytes(intbits); } public static byte [] getBytes(字符串數據,字符串charsetname){charset charset = charset.forname(charsetname);返回data.getBytes(charset); } public static byte [] getBytes(string data){return getBytes(data,“ gbk”); } public static short 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&(bytes [1] << 8))| (0xff0000&(bytes [2] << 16))| (0xff000000&(bytes [3] << 24)); } public static long getlong(byte [] bytes){return(0xffl&(long)bytes [0])| (0xff00l&((long)字節[1] << 8))| (0xff0000l&((long)字節[2] << 16))| (0xff000000l&((long)字節[3] << 24))| (0xff00000000l&((long)字節[4] << 32))| (0xff0000000000l&((long)字節[5] << 40))| (0xff000000000000l&((long)字節[6] << 48))| (0xFF00000000000000L&((長)字節[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,字符串charsetname){return new String(bytes,charset.forname(charsetName)); } public static String getString(byte [] bytes){return get getString(bytes,gbk'); } public static void main(string [] args){簡短s = 122; int i = 122;長L = 1222222; char c ='a'; float f = 122.22f; double d = 122.22;字符串=“我是好孩子”; 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基本類型與字節數組之間相互轉換方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。 ,也希望大家多多支持武林網。