Java 基本类型与 octet 数组之间相互转换 , 刚刚写的
package cn.teaey.utils; import java.nio.charset.charset; public class byteutil {public static byte [] getBytes (data courte) {octet [] bytes = new octet [2]; Bytes [0] = (byte) (data & 0xff); octets [1] = (octet) ((data & 0xff00) >> 8); RETOUR des octets; } octet statique publique [] getBytes (données char) {octet [] bytes = new octet [2]; octets [0] = (octet) (données); octets [1] = (octet) (données >> 8); RETOUR des octets; } byte statique publique [] GetBytes (données int) {octet [] bytes = new Byte [4]; Bytes [0] = (byte) (data & 0xff); octets [1] = (octet) ((data & 0xff00) >> 8); octets [2] = (octet) ((data & 0xff0000) >> 16); octets [3] = (octet) ((data & 0xff000000) >> 24); RETOUR des octets; } octet statique public [] GetBytes (données longues) {octet [] bytes = new octet [8]; Bytes [0] = (byte) (data & 0xff); octets [1] = (octet) ((données >> 8) & 0xff); octets [2] = (octet) ((données >> 16) & 0xff); octets [3] = (octet) ((données >> 24) & 0xff); octets [4] = (octet) ((données >> 32) & 0xff); octets [5] = (octet) ((données >> 40) & 0xff); octets [6] = (octet) ((données >> 48) & 0xff); octets [7] = (octet) ((données >> 56) & 0xff); RETOUR des octets; } octet statique public [] getBytes (data float) {int IntBits = float.floAtTointBits (data); return GetBytes (intbits); } octet statique public [] GetBytes (Double Data) {long intbits = double.doubleTolongBits (données); return GetBytes (intbits); } octet statique publique [] GetBytes (Data String Data, String charSetName) {Charset charSet = charSet.Forname (charSetName); return data.getBytes (charse); } octet statique public [] GetBytes (Data String) {return getBytes (data, "gbk"); } public static short getShort (byte [] octets) {return (short) ((0xff & bytes [0]) | (0xff00 & (bytes [1] << 8))); } public static char getchar (byte [] octets) {return (char) ((0xff & bytes [0]) | (0xff00 & (bytes [1] << 8)))); } public static int getInt (byte [] octets) {return (0xff & bytes [0]) | (0xff00 et (octets [1] << 8)) | (0xff0000 et (octets [2] << 16)) | (0xff000000 et (octets [3] << 24)); } public static long getLong (byte [] octets) {return (0xffl & (long) octets [0]) | (0xff00l & ((long) octets [1] << 8)) | (0xff0000l & ((long) octets [2] << 16)) | (0xff000000l & ((long) octets [3] << 24)) | (0xff00000000l et ((long) octets [4] << 32)) | (0xff0000000000l et ((long) octets [5] << 40)) | (0xff000000000000l & ((long) octets [6] << 48)) | (0xFF00000000000000L & ((long) octets [7] << 56)); } public static float getFloat (byte [] octets) {return float.intbitstofloat (getInt (bytes)); } public static double getDouble (octet [] octets) {long l = getLong (octets); 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 [] octets) {return getString (octets, "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 基本类型与 octet 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。