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基本类型与字节数组之间相互转换方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持武林网。,也希望大家多多支持武林网。