Exemplos são os seguintes:
classe pública DatatyPeChangeHelper { / ** * Converta um byte de byte único em um Int * * @param b * byte * @return convert resultado * / public static int não assinadoByteToint (byte b) {return (int) b & 0xff; } / ** * Converta um byte de byte único em um número hexadecimal * * @param b * byte * @return convert resultado * / public static string bytetohex (byte b) {int i = b & 0xff; return integer.tohexstring (i); } / ** * Converta uma matriz de 4bytes em um buffer de 32 bits int * * @param buf * bytes * A posição em @param byte [] onde a conversão inicia * @return convert resultado * / public static Long4ByTestintint (byte [] BUF, int posse) {int FirstByte = 0; int SecondByte = 0; int terceirobyte = 0; int Quarthbyte = 0; int index = pos; FirstByte = (0x000000ff & ((int) buf [index])); SecondByte = (0x000000ff & ((int) buf [índice + 1])); terceirobyte = (0x000000ff & ((int) buf [index + 2])); Quartothbyte = (0x000000ff & ((int) buf [índice + 3])); índice = índice + 4; return ((longo) (FirstByte << 24 | SecondByte << 16 | Terceirobyte << 8 | Quarto))) & 0xffffffl; } / ** * Converta a matriz de 16 bits a bytes * * @param s * curto * @return byte [] O comprimento é 2 * * / public static byte [] shortTobyteArray (curto s) {byte [] alvos = novo byte [2]; for (int i = 0; i <2; i ++) {int offset = (Targets.length - 1 - i) * 8; alvos [i] = (byte) ((s >>> deslocamento) e 0xff); } retornar alvos; } / ** * Converta números inteiros de 32 bits em matrizes de bytes com comprimento 4 * * @param s * int * @return byte [] * * / public static byte [] inttobytearray (int s) {byte [] alvos = new Byte [2]; for (int i = 0; i <4; i ++) {int offset = (Targets.length - 1 - i) * 8; alvos [i] = (byte) ((s >>> deslocamento) e 0xff); } retornar alvos; } / ** * long to byte [] * * @param s * long * @return byte [] * * / public static byte [] longTobyteArray (long s) {byte [] alvos = novo byte [2]; for (int i = 0; i <8; i ++) {int offset = (Targets.length - 1 - i) * 8; alvos [i] = (byte) ((s >>> deslocamento) e 0xff); } retornar alvos; } / ** 32 bits int para byte []* / public static byte [] int2byte (int res) {byte [] alvos = novo byte [4]; alvos [0] = (byte) (res & 0xff); // alvos de dígitos mais baixos [1] = (byte) ((res >> 8) e 0xff); // Segundo alvos de baixo dígito [2] = (byte) (res >> 16) & 0xff); retornar alvos; } / ** * Converta uma matriz de bytes de comprimento 2 em 16 bits int * * @param res * byte [] * @return int * * / public static int byte2int (byte [] res) {// res = InversionByte (res); // Dados de byte são deslocados 24 bits deixados para 0x ?? 000000 e depois deslocados 8 bits para a direita para 0x00? ((res [1] << 8) e 0xff00); // | Indica alvos ambiente ou retorno; }}O exposto acima é o conteúdo completo do método de implementação de conversão de Java Byte Array e Int, longo, curto, byte trazido a você. Espero que todos apoiem mais wulin.com ~