Java 基本类型与 byte 数组之间相互转换 , 刚刚写的
pacote cn.teaey.utils; importar java.nio.charset.charset; public class Byteutil {public static byte [] getBytes (dados curtos) {byte [] bytes = novo byte [2]; bytes [0] = (byte) (dados e 0xff); bytes [1] = (byte) ((dados & 0xff00) >> 8); retornar bytes; } public static byte [] getBytes (Char Data) {byte [] bytes = new Byte [2]; bytes [0] = (byte) (dados); bytes [1] = (byte) (dados >> 8); retornar bytes; } public static byte [] getBytes (int dados) {byte [] bytes = new Byte [4]; bytes [0] = (byte) (dados e 0xff); bytes [1] = (byte) ((dados & 0xff00) >> 8); bytes [2] = (byte) ((dados e 0xff0000) >> 16); bytes [3] = (byte) ((dados e 0xff000000) >> 24); retornar bytes; } public static byte [] getBytes (long Data) {byte [] bytes = new Byte [8]; bytes [0] = (byte) (dados e 0xff); bytes [1] = (byte) ((dados >> 8) e 0xff); bytes [2] = (byte) ((dados >> 16) e 0xff); bytes [3] = (byte) ((dados >> 24) e 0xff); bytes [4] = (byte) ((dados >> 32) e 0xFF); bytes [5] = (byte) ((dados >> 40) e 0xFF); bytes [6] = (byte) ((dados >> 48) e 0xff); bytes [7] = (byte) ((dados >> 56) e 0xff); retornar bytes; } public static byte [] getBytes (dados float) {int intbits = float.floattointbits (dados); retornar getBytes (intbits); } public static byte [] getBytes (dados duplos) {long intbits = duplo.doubleTolongbits (dados); retornar getBytes (intbits); } public static byte [] getBytes (dados da string, string charsetName) {charset charset = charset.ForName (charsetName); retornar data.getBytes (charset); } public static byte [] getBytes (string dados) {return getBytes (dados, "gbk"); } public static curto getshort (byte [] bytes) {return (curto) ((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 & (((longo) bytes [1] << 8)) | (0xff0000l & (((longo) bytes [2] << 16)) | (0XFF0000L & ((Long) bytes [3] << 24)) | (0XFF000000L & ((Longo) bytes [4] << 32)) | (0XFF00000000L & ((Longo) bytes [5] << 40)) | (0XFF0000000000L & ((Longo) bytes [6] << 48)) | (0XFF000000000000L & ((LONG) bytes [7] << 56)); } public static float getfloat (byte [] bytes) {return float.intbitstofloat (getint (bytes)); } public static duplo getDouble (byte [] bytes) {long l = getlong (bytes); System.out.println (l); retornar duplo.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'; flutuar f = 122,22f; duplo 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 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。