Java 基本类型与 Byte 数组之间相互转换 , 刚刚写的
Paket cn.Teaey.utils; import Java.nio.Charset.Charset; öffentliche Klasse byteutil {public static byte [] getBytes (kurze Daten) {byte [] bytes = new Byte [2]; Bytes [0] = (Byte) (Daten & 0xff); Bytes [1] = (Byte) ((Daten & 0xff00) >> 8); Rückkehr Bytes; } public static byte [] getBytes (char data) {byte [] bytes = new Byte [2]; Bytes [0] = (Byte) (Daten); Bytes [1] = (Byte) (Daten >> 8); Rückkehr Bytes; } public static byte [] getBytes (int data) {byte [] bytes = new Byte [4]; Bytes [0] = (Byte) (Daten & 0xff); Bytes [1] = (Byte) ((Daten & 0xff00) >> 8); Bytes [2] = (Byte) ((Daten & 0xff0000) >> 16); Bytes [3] = (Byte) ((Daten & 0xff000000) >> 24); Rückkehr Bytes; } public static byte [] getBytes (lange Daten) {byte [] bytes = new Byte [8]; Bytes [0] = (Byte) (Daten & 0xff); Bytes [1] = (Byte) ((Daten >> 8) & 0xff); Bytes [2] = (Byte) ((Daten >> 16) & 0xff); Bytes [3] = (Byte) ((Daten >> 24) & 0xff); Bytes [4] = (Byte) ((Daten >> 32) & 0xff); Bytes [5] = (Byte) ((Daten >> 40) & 0xff); Bytes [6] = (Byte) ((Daten >> 48) & 0xff); Bytes [7] = (Byte) ((Daten >> 56) & 0xff); Rückkehr Bytes; } public static byte [] getBytes (float data) {int intbits = float.floattointbits (Daten); return getBytes (intbits); } public static byte [] getBytes (doppelte Daten) {long intbits = double.doubletolongbits (Daten); return getBytes (intbits); } public static byte [] getBytes (String -Daten, String charSetName) {charSet charSet = charset.forname (charSetName); return data.getBytes (charSet); } public static byte [] getBytes (String -Daten) {return getBytes (Daten, "gbk"); } public static Short getShort (byte [] bytes) {return (kurz) ((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 & (lang) Bytes [0]) | (0xff00l & ((lange) Bytes [1] << 8)) | (0xff0000l & ((lange) Bytes [2] << 16)) | (0xff0000l & ((lange) Bytes [3] << 24)) | (0xff000000l & ((lange) Bytes [4] << 32)) | (0xff00000000l & ((lange) Bytes [5] << 40)) | (0xff0000000000L & ((lange) Bytes [6] << 48)) | (0xff000000000000L & ((lange) 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) {neuer String zurückgeben (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; lang L = 1222222; char c = 'a'; float f = 122,22f; Doppel 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 数组之间相互转换方法就是小编分享给大家的全部内容了 , 希望能给大家一个参考 , 也希望大家多多支持武林网。 也希望大家多多支持武林网。 也希望大家多多支持武林网。