Java 基本类型与 byte 数组之间相互转换 刚刚写的 刚刚写的
paquete cn.teaey.utils; import java.nio.charset.charset; public class byteutil {public static byte [] getBytes (datos cortos) {byte [] bytes = new byte [2]; bytes [0] = (byte) (datos y 0xff); bytes [1] = (byte) ((datos y 0xff00) >> 8); bytes de devolución; } public static byte [] getbytes (char data) {byte [] bytes = new byte [2]; bytes [0] = (byte) (datos); bytes [1] = (byte) (datos >> 8); bytes de devolución; } public static byte [] getBytes (int data) {byte [] bytes = new byte [4]; bytes [0] = (byte) (datos y 0xff); bytes [1] = (byte) ((datos y 0xff00) >> 8); bytes [2] = (byte) ((datos y 0xff0000) >> 16); bytes [3] = (byte) ((datos y 0xff000000) >> 24); bytes de devolución; } public static byte [] getBytes (data largo) {byte [] bytes = new byte [8]; bytes [0] = (byte) (datos y 0xff); bytes [1] = (byte) ((datos >> 8) y 0xff); bytes [2] = (byte) ((datos >> 16) y 0xff); bytes [3] = (byte) ((datos >> 24) y 0xff); bytes [4] = (byte) ((datos >> 32) y 0xff); bytes [5] = (byte) ((datos >> 40) y 0xff); bytes [6] = (byte) ((datos >> 48) y 0xff); bytes [7] = (byte) ((datos >> 56) y 0xff); bytes de devolución; } public static byte [] getbytes (datos de flotación) {int intbits = float.floatTointBits (datos); return getBytes (intbits); } public static byte [] getBytes (datos dobles) {long intbits = double.Doubletolongbits (datos); return getBytes (intbits); } public static byte [] getBytes (string data, string charsetName) {charset charset = charset.forname (charsetName); return data.getBytes (charset); } public static byte [] getBytes (string data) {return getBytes (datos, "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 y (bytes [1] << 8)) | (0xff0000 y (bytes [2] << 16)) | (0xff000000 y (bytes [3] << 24)); } public static long getlong (byte [] bytes) {return (0xffl & (long) bytes [0]) | (0xff00l & (((largo) bytes [1] << 8)) | (0xff0000l & (((largo) bytes [2] << 16)) | (0xff000000l & (((largo) bytes [3] << 24)) | (0xff0000000000L & (((largo) bytes [4] << 32)) | (0xff000000000000L & (((largo) bytes [5] << 40)) | (0xff00000000000000L & (((largo) bytes [6] << 48)) | (0xff0000000000000000L & (((largo) 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); devolver double.longbitstodouble (l); } public static string getString (byte [] bytes, cadena charsetName) {return new String (bytes, charset.forname (charsetName)); } public static string getTring (byte [] bytes) {return getString (bytes, "gbk"); } public static void main (string [] args) {corto s = 122; int i = 122; largo L = 1222222; char c = 'a'; flotante F = 122.22f; doble d = 122.22; Cadena 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 (cadena); 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 (getTring (getBytes (string))); }}以上这篇 java 基本类型与 byte 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 也希望大家多多支持武林网。