Ejemplos simples de Java Basics Byte [] Convertirse entre sí con varios tipos de datos
Aquí hay un ejemplo de la conversión de los tipos de byte [] a larga, int, doble, flotación, corta, cahr, objeto, tipos de cadenas,
Durante el desarrollo del socket, generalmente necesita convertir algunos valores específicos (estos valores pueden ser varios tipos de Java) en el tipo de byte []. Por esta razón, he resumido el siguiente ejemplo y lo publiqué para poder leerlo con frecuencia:
Public Class TestCase { /*** Conversión de matriz corta a bytes. */ public static byte [] ShortTobyte (número corto) {int temp = number; byte [] b = nuevo byte [2]; for (int i = 0; i <b.length; i ++) {b [i] = new Integer (temp & 0xff) .byTeValue (); // Guardar el bit más bajo en el bit más bajo Temp = temp >> 8; // mover 8 bits a la derecha} return b; } /*** Conversión de la matriz de bytes a corta. */ public static short bytetoshort (byte [] b) {corto s = 0; corto s0 = (corto) (b [0] y 0xff); // El bit más bajo corto s1 = (corto) (b [1] y 0xff); S1 << = 8; s = (corto) (S0 | S1); regreso s; } /*** Conversión de int a matriz de bytes. */ public static byte [] inttobyte (int number) {int temp = number; byte [] b = nuevo byte [4]; for (int i = 0; i <b.length; i ++) {b [i] = new Integer (temp & 0xff) .byTeValue (); // Guardar el bit más bajo en el bit más bajo Temp = temp >> 8; // mover 8 bits a la derecha} return b; } /*** Conversión de la matriz de bytes a int. */ public static int bytetoint (byte [] b) {int s = 0; int s0 = b [0] & 0xff; // El bit más bajo 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; regreso s; } / *** Convierta el tipo largo en matriz de byte* / public static byte [] longTobyte (número largo) {long temp = number; byte [] b = nuevo byte [8]; for (int i = 0; i <b.length; i ++) {b [i] = new Long (temp & 0xff) .byTeValue (); // Guarde el bit más bajo en el bit más bajo temp = temp // >> 8; // Mover 8 bits a la derecha} return b; } /*** Conversión de la matriz de bytes a largo. */ public static long bytetolong (byte [] b) {long s = 0; largo s0 = b [0] & 0xff; // El bit más bajo largo S1 = B [1] largo S2 = B [2] & 0xff; largo S3 = B [3] y 0xff; largo S4 = B [4] & 0xff; // El bit más bajo largo S5 = B [5] & 0xff; largo S6 = B [6] y 0xff; largo S7 = B [7] y 0xff; // S0 no cambia 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; regreso s; } /*** Conversión de la matriz doble a byte. */ public static byte [] doubleTobyte (doble num) {byte [] b = nuevo byte [8]; long l = doble.doubletolongbits (num); para (int i = 0; i <8; i ++) {b [i] = new Long (l) .byTevalue (); l = l >> 8; } retorno b; } /*** Conversión de la matriz de bytes al doble. */ public static double getDouble (byte [] b) {long m; m = b [0]; m & = 0xff; m | = ((largo) B [1] << 8); m & = 0xffffffff; m | = ((largo) B [2] << 16); m & = 0xffffffffff; m | = ((largo) B [3] << 24); m & = 0xffffffffffl; m | = ((largo) B [4] << 32); m & = 0xffffffffffffl; m | = ((largo) B [5] << 40); m & = 0xffffffffffffl; m | = ((largo) B [6] << 48); m & = 0xffffffffffffffl; m | = ((largo) B [7] << 56); return double.longbitStodouble (m); } /*** Conversión de la matriz de flotación a byte. * /public static void floattobyte (Float x) {// Convertir float.floatTointBits (f) a int} /*** Conversión de la matriz de bytes a la flotación. */ public static float getfloat (byte [] b) {// 4 bytes int acum = 0; for (int shiftby = 0; shiftby <4; shiftby ++) {acum | = (b [shiftby] & 0xff) << shiftby * 8; } return float.intbitstofloat (acces); } /*** Conversión de Char a Byte Array. */ public static byte [] Chartobyte (char c) {byte [] b = new byte [2]; b [0] = (byte) ((c & 0xff00) >> 8); b [1] = (byte) (c & 0xff); regreso B; } /*** Conversión de la matriz de bytes a char. */ public static char bytetochar (byte [] b) {char c = (char) (((b [0] & 0xff) << 8) | (b [1] & 0xff)); regreso c; } /*** Conversión de la matriz de cadena a byte. */ public static byte [] StringTobyte (String Str) lanza UnspportedEncodingException {return str.getBytes ("gbk"); } /*** Conversión de la matriz de bytes a la cadena. */ public static string byTESToString (byte [] str) {string keyword = null; prueba {palabra clave = nueva cadena (str, "gbk"); } catch (UnsupportedEncodingException e) {E.PrintStackTrace (); } Palabra clave de retorno; } / *** La conversión del objeto a la matriz de byte* / @test public void testObject2byTearray () lanza ioexception, classNotFoundException {// object obj = ""; Entero [] obj = {1, 3, 4}; // // Objeto a ByteArray byteArRaReOutputStream bo = new ByTearRaReOutputStream (); ObjectOutputStream oo = new ObjectOutputStream (bo); oo.writeObject (obj); byte [] bytes = bo.tobytearray (); Bo.close (); oo.close (); System.out.println (Arrays.ToString (bytes)); Entero [] intarr = (entero []) testbyTearray2Object (bytes); System.out.println (arrays.aslist (intarr)); byte [] b2 = inttobyte (123); System.out.println (Arrays.ToString (B2)); int a = byteToint (b2); System.out.println (a); } /*** Conversión de la matriz de byte a objeto. */ Private Object testByTearRay2Object (byte [] bytes) lanza ioexception, classNotFoundException {// byte [] bytes = null; Obj obj; // byteArray to Object bytearrayInputStream bi = new bytearrayInputStream (bytes); ObjectInputStream oi = new ObjectInputStream (BI); obj = oi.readObject (); bi.close (); oi.close (); System.out.println (obj); regresar obj; }}Gracias por leer, espero que pueda ayudarte. ¡Gracias por su apoyo para este sitio!