Los ejemplos son los siguientes:
clase pública DataTyPechangeHelper { / ** * Convierta un byte de byte único en un int * * * @param b * byte * @return convert resultado * / public static int unsignedbytetint (byte b) {return (int) b & 0xff; } / ** * Convierta un byte de byte único a un número hexadecimal * * @param b * byte * @return convertir resultado * / public static string bytetohex (byte b) {int i = b & 0xff; return integer.tohexstring (i); } / ** * Convierta una matriz de 4 bytes en un int * * * @param buf * bytes bytes * la posición en @param byte [] donde la conversión comienza * @return convertir resultado * / public static long sin firmed4bytesteint (byte [] buf, int pos) {int firstyte = 0; int Secondbyte = 0; int terctbyte = 0; int FourThByte = 0; int index = pos; FirstByte = (0x000000ff & ((int) buf [index])); Secondbyte = (0x000000ff & ((int) BUF [índice + 1])); ThirdByte = (0x000000ff & ((int) BUF [índice + 2])); CuarThByte = (0x000000ff & ((int) BUF [índice + 3])); índice = índice + 4; return ((long) (FirstByte << 24 | Secondbyte << 16 | Thirdbyte << 8 | Cuarto)) y 0xffffffl; } / ** * Convertir la matriz corta de byte de 16 bits * * @param s * short * @return byte [] la longitud es 2 * * / public static byte [] shorttobytearray (corta s) {byte [] Targets = new byte [2]; for (int i = 0; i <2; i ++) {int offset = (targets.length - 1 - i) * 8; Objetivos [i] = (byte) ((s >>> compensación) y 0xff); } objetivos de retorno; } / ** * Convierta enteros de 32 bits en matrices de bytes con longitud 4 * * @param s * int * @return byte [] * * / public static byte [] inttobytearray (int s) {byte [] targets = new byte [2]; for (int i = 0; i <4; i ++) {int offset = (targets.length - 1 - i) * 8; Objetivos [i] = (byte) ((s >>> compensación) y 0xff); } objetivos de retorno; } / ** * Long to byte [] * * @param s * long * @return byte [] * * / public static byte [] longtobytearray (long s) {byte [] targets = new byte [2]; for (int i = 0; i <8; i ++) {int offset = (targets.length - 1 - i) * 8; Objetivos [i] = (byte) ((s >>> compensación) y 0xff); } objetivos de retorno; } / ** 32 bit int to byte []* / public static byte [] int2byte (int res) {byte [] objetivos = new Byte [4]; objetivos [0] = (byte) (res & 0xff); // Los objetivos de dígitos más bajos [1] = (byte) ((res >> 8) y 0xff); // Segundo dígitos de dígitos [2] = (byte) ((res >> 16) y 0xff); // Los objetivos de la segunda posición alta [3] = (byte) (res >>> 24); // la posición más alta, el cambio de posición al máximo. objetivos de retorno; } / ** * Convierta una matriz de bytes de longitud 2 en 16 bits int * * @param res * byte [] * @return int * * / public static int byte2int (byte [] res) {// res = inversionByte (res); // Los datos de byte se desplazan 24 bits izquierdos a 0x ?? 000000, y luego cambian 8 bits a la derecha a 0x00 ?? 00000 int Targets = (res [0] & 0xff) | ((res [1] << 8) y 0xff00); // | Indica objetivos ambientales o de retorno; }}Lo anterior es el contenido completo del método de implementación de conversión de Java Byte Array e int, Long, Short, Byte traído a usted. Espero que todos apoyen a Wulin.com más ~