Java 基本类型与 byte 数组之间相互转换 , 刚刚写的
paket cn.teaey.utils; import java.nio.charset.charset; byteutil kelas publik {public static byte [] getBytes (data pendek) {byte [] bytes = byte baru [2]; bytes [0] = (byte) (data & 0xff); bytes [1] = (byte) ((data & 0xff00) >> 8); Byte kembali; } public static byte [] getBytes (data char) {byte [] bytes = byte baru [2]; bytes [0] = (byte) (data); bytes [1] = (byte) (data >> 8); Byte kembali; } public static byte [] getBytes (int data) {byte [] bytes = byte baru [4]; bytes [0] = (byte) (data & 0xff); bytes [1] = (byte) ((data & 0xff00) >> 8); bytes [2] = (byte) ((data & 0xff0000) >> 16); bytes [3] = (byte) ((Data & 0xff000000) >> 24); Byte kembali; } public static byte [] getBytes (data panjang) {byte [] bytes = byte baru [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); Byte kembali; } public static byte [] getBytes (float data) {int intbits = float.floattointbits (data); return getBytes (intbits); } public static byte [] getBytes (data ganda) {long intbits = double.doubleToLongBits (data); return getBytes (intbits); } public static byte [] getBytes (data string, string charsetName) {charset charset = charset.forname (charsetName); return data.getbytes (charset); } public static byte [] getBytes (data string) {return getBytes (data, "gbk"); } public static short getShort (byte [] bytes) {return (pendek) ((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 & ((panjang) bytes [1] << 8)) | (0xff0000l & ((panjang) bytes [2] << 16)) | (0xff000000l & ((panjang) bytes [3] << 24)) | (0xff00000000l & ((panjang) bytes [4] << 32)) | (0xFF0000000000L & ((Long) Bytes [5] << 40)) | (0xff000000000000L & ((Long) Bytes [6] << 48)) | (0xFF00000000000000L & ((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); return 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'; float f = 122.22f; Double D = 122.22; String 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 (getChar (getBytes (c))); System.out.println (getFloat (getBytes (f))); System.out.println (getDouble (getBytes (d))); System.out.println (getString (getBytes (string))); }}以上这篇 java 基本类型与 byte 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。