Einfache Beispiele für Java -Basics -Byte [] Konvertieren gegenseitig mit verschiedenen Datentypen
Hier ist ein Beispiel für die Umwandlung von Byte [] -Typen in lange, int, doppelte, float, Short, CAHR, Objekt, Stringentypen,
Während der Socket -Entwicklung müssen Sie normalerweise einige spezifische Werte (diese Werte können verschiedene Java -Typen sein) in Byte [] -Typ umwandeln. Aus diesem Grund habe ich das folgende Beispiel zusammengefasst und es gepostet, damit ich es häufig lesen kann:
public class TestCase { /*** Kurz- bis Byte -Array -Konvertierung. */ public static byte [] ShortTobyte (Kurznummer) {int temp = number; Byte [] B = neues Byte [2]; für (int i = 0; i <B.Length; i ++) {b [i] = new Integer (temp & 0xff) .ByteValue (); // Speichern Sie das niedrigste Bit in der niedrigsten Temp = Temp >> 8; // 8 Bit nach rechts} return b; } /*** Konvertierung von Byte -Array nach kurz. */ public static Short Bytetoshort (Byte [] b) {Short S = 0; kurz S0 = (kurz) (B [0] & 0xff); // Das niedrigste Bit -Short S1 = (kurz) (B [1] & 0xff); S1 << = 8; S = (kurz) (S0 | S1); Rückkehr s; } /*** Konvertierung von int zu Byte -Array. */ public static byte [] inttobyte (int number) {int temp = number; Byte [] B = neues Byte [4]; für (int i = 0; i <B.Length; i ++) {b [i] = new Integer (temp & 0xff) .ByteValue (); // Speichern Sie das niedrigste Bit in der niedrigsten Temp = Temp >> 8; // 8 Bit nach rechts} return b; } /*** Konvertierung vom Byte -Array nach int. */ public static int byToint (byte [] b) {int s = 0; int s0 = b [0] & 0xff; // das niedrigste Bit int S1 = B [1] & 0xff; int s2 = b [2] & 0xff; int s3 = b [3] & 0xff; S3 << = 24; S2 << = 16; S1 << = 8; S = S0 | S1 | S2 | S3; Rückkehr s; } / *** den langen Typ in Byte -Array umwandeln Byte [] B = neues Byte [8]; für (int i = 0; i <B.Length; i ++) {b [i] = new Long (temp & 0xff) .ByteValue (); // Speichern Sie das niedrigste Bit in der niedrigsten Bit -Temp = temp // >> 8; // 8 Bit nach rechts} return b; } /*** Konvertierung von Byte -Array zu Long. */ public static long bytetolong (byte [] b) {long s = 0; langes S0 = B [0] & 0xff; // Das niedrigste Bit lange s1 = b [1] lang S2 = B [2] & 0xff; langer S3 = B [3] & 0xff; langes S4 = B [4] & 0xff; // Das niedrigste Bit lange S5 = B [5] & 0xff; langer S6 = B [6] & 0xff; langer S7 = B [7] & 0xff; // S0 ist unverändert S1 << = 8; S2 << = 16; S3 << = 24; S4 << = 8 * 4; S5 << = 8 * 5; S6 << = 8 * 6; S7 << = 8 * 7; S0 | S1 | S2 | S3 | s4 | s5 | s6 | S7; Rückkehr s; } /*** Konvertierung von Double zum Byte -Array. */ public static byte [] doubleTobyte (double num) {byte [] b = new Byte [8]; long l = double.doubletolongbits (num); für (int i = 0; i <8; i ++) {b [i] = new Long (l) .ByteValue (); l = l >> 8; } return b; } /*** Konvertierung von Byte -Array in Doppel. */ public static double getDouble (byte [] b) {long m; M = B [0]; M & = 0xff; m | = ((lang) b [1] << 8); M & = 0xffffffff; m | = ((lang) b [2] << 16); m & = 0xffffffffff; m | = ((lang) b [3] << 24); m & = 0xffffffffffl; m | = ((lang) b [4] << 32); m & = 0xffffffffffffffl; m | = ((lang) b [5] << 40); m & = 0xffffffffffffffl; m | = ((lang) b [6] << 48); m & = 0xffffffffffffffffl; m | = ((lang) b [7] << 56); return double.longbitStodouble (m); } /*** Konvertierung vom Float zum Byte -Array. * /public static void floattobyte (float x) {// Float.Floattointbits (f) in int} /*** Konvertierung vom Byte -Array zum Schweben. */ public static float getfloat (byte [] b) {// 4 bytes int akkum = 0; für (int Shiftby = 0; Shiftby <4; Shiftby ++) {scap | } return float.intbitstofloat (akkum); } /*** Konvertierung von char zu byte Array. */ public static byte [] chartobyte (char c) {byte [] b = new Byte [2]; B [0] = (Byte) ((c & 0xff00) >> 8); B [1] = (Byte) (C & 0xff); Rückkehr B; } /*** Konvertierung von Byte -Array in char. */ public static char bytetocher (byte [] b) {char c = (char) (((b [0] & 0xff) << 8) | (b [1] & 0xff)); Rückkehr C; } /*** Konvertierung von String zum Byte -Array. */ public static byte [] stringTobyte (String str) löscht nicht unterstützte AnscodingException {return str.getBytes ("gbk"); } /*** Konvertierung vom Byte -Array zu String. */ public static String byTestoString (byte [] str) {String keyword = null; try {keyword = new String (str, "gbk"); } catch (unportedenCodingException e) {e.printstacktrace (); } Keyword zurückgeben; } / *** Konvertierung vom Objekt zu Byte -Array* / @test public void testObject2ByTearray () löst IOException aus, classNotFoundException {// Objekt obj = ""; Integer [] obj = {1, 3, 4}; // // Objekt zu bytearray bytearrayoutputStream bo = new bytearrayoutputStream (); ObjectOutputStream oo = new ObjectOutputStream (BO); oo.writeObject (obj); byte [] bytes = bo.tobytearray (); bo.close (); oo.close (); System.out.println (Arrays.toString (Bytes)); Integer [] intarr = (Integer []) testByTearray2Object (Bytes); System.out.println (arrays.aslist (intarr)); byte [] b2 = inttobyte (123); System.out.println (Arrays.toString (B2)); int a = byToint (b2); System.out.println (a); } /*** Konvertierung vom Byte -Array zum Objekt. */ private Object testByTearray2Object (byte [] bytes) löst ioException, classNotFoundException {// byte [] bytes = null aus; Objekt obj; // bytearray zu Object bytearrayInputStream bi = new bytearrayInputStream (Bytes); ObjectInputStream oi = new ObjectInputStream (bi); obj = oi.readObject (); bi.close (); oi.close (); System.out.println (OBJ); Rückkehr obj; }}Danke fürs Lesen, ich hoffe, es kann Ihnen helfen. Vielen Dank für Ihre Unterstützung für diese Seite!