Java 基本类型与 Byte 数组之间相互转换 , 刚刚写的
пакет cn.teaey.utils; import java.nio.charset.charset; public class byteutil {public static byte [] getbytes (короткие данные) {byte [] bytes = new Byte [2]; байты [0] = (byte) (data & 0xff); байты [1] = (byte) ((data & 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) ((data & 0xff00) >> 8); байты [2] = (байт) ((data & 0xff0000) >> 16); байты [3] = (байт) ((данные и 0xff000000) >> 24); вернуть байты; } public static byte [] getbytes (long data) {byte [] bytes = new Byte [8]; байты [0] = (byte) (data & 0xff); байты [1] = (byte) ((data >> 8) & 0xff); байты [2] = (байт) ((данные >> 16) & 0xff); байты [3] = (byte) ((data >> 24) & 0xff); байты [4] = (byte) ((data >> 32) & 0xff); байты [5] = (байт) ((данные >> 40) & 0xff); байты [6] = (byte) ((data >> 48) & 0xff); байты [7] = (байт) ((данные >> 56) & 0xff); вернуть байты; } public static byte [] getBytes (float data) {int intbits = float.floattointbits (data); вернуть GetBytes (INTBITS); } public static byte [] getBytes (двойные данные) {long intbits = double.doubletolongbits (data); вернуть GetBytes (INTBITS); } public Static Byte [] getBytes (String Data, String charsetName) {charset charset = charset.forname (charsetname); вернуть data.getbytes (charset); } public Static Byte [] getBytes (String Data) {return getBytes (data, "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 & (байты [1] << 8)) | (0xff0000 & (байты [2] << 16)) | (0xff000000 и (байты [3] << 24)); } public static long getLong (byte [] bytes) {return (0xffl & (long) bytes [0]) | (0xff00l & ((Long) Bytes [1] << 8)) | (0xff0000L & ((Long) Bytes [2] << 16)) | (0xff000000L & ((Long) Bytes [3] << 24)) | (0xff0000000000L & ((Long) Bytes [4] << 32)) | (0xff000000000000L & ((Long) Bytes [5] << 40)) | (0xff00000000000000L & ((Long) Bytes [6] << 48)) | (0xff0000000000000000L & ((Long) Bytes [7] << 56)); } public static float getfloat (byte [] bytes) {return float.intbitstofloat (getInt (bytes)); } public static double getDouble (byte [] bytes) {long l = getlong (байты); System.out.println (l); вернуть 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 (байты, "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 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。