Contohnya adalah sebagai berikut:
DataPechangeHelper kelas publik { / ** * Konversi byte byte tunggal menjadi int 32-bit * * @param b * byte * @return convert hasil * / int statis public unsignedbyToint (byte b) {return (int) b & 0xff; } / ** * Konversi byte byte tunggal ke nomor hexadecimal * * @param b * byte * @return convert hasil * / public static string bytetoHex (byte b) {int i = b & 0xff; return integer.tohexstring (i); } / ** * Konversi array 4bytes menjadi 32-bit int * * @param buf * bytes buffer * Posisi dalam @param byte [] di mana konversi dimulai * @return convert hasil * / public static long unsigned4bytoint (byte [] buf, int pos) {int firstbyte = 0; Int SecondByte = 0; Int ThirdByte = 0; int fourthbyte = 0; int index = pos; firstbyte = (0x000000ff & ((int) buf [index])); SecondByte = (0x000000ff & ((int) buf [indeks + 1])); fhirdByte = (0x000000ff & ((int) buf [index + 2])); fourthbyte = (0x000000ff & ((int) buf [index + 3])); indeks = indeks + 4; return ((long) (firstbyte << 24 | SecondByte << 16 | fhirdbyte << 8 | fourthbyte)) & 0xffffffl; } / ** * Konversi array pendek 16-bit ke byte * * @param s * pendek * @return byte [] panjang adalah 2 * * / byte statis publik [] shorttobyteArray (pendek s) {byte [] target = byte baru [2]; untuk (int i = 0; i <2; i ++) {int offset = (targets.length - 1 - i) * 8; target [i] = (byte) ((s >>> offset) & 0xff); } mengembalikan target; } / ** * Konversi bilangan bulat 32-bit menjadi array byte dengan panjang 4 * * @param s * int * @return byte [] * * / public static byte [] inttobyteArray (int s) {byte [] target = byte baru [2]; untuk (int i = 0; i <4; i ++) {int offset = (targets.length - 1 - i) * 8; target [i] = (byte) ((s >>> offset) & 0xff); } mengembalikan target; } / ** * Long to byte [] * * @param s * long * @return byte [] * * / public static byte [] longtoByTeArray (long s) {byte [] target = byte baru [2]; untuk (int i = 0; i <8; i ++) {int offset = (targets.length - 1 - i) * 8; target [i] = (byte) ((s >>> offset) & 0xff); } mengembalikan target; } / ** 32-bit int ke byte []* / public static byte [] int2byte (int res) {byte [] target = byte baru [4]; target [0] = (byte) (res & 0xff); // target digit terendah [1] = (byte) ((res >> 8) & 0xff); // target digit rendah kedua [2] = (byte) ((res >> 16) & 0xff); // Target posisi tertinggi kedua [3] = (res >>. target pengembalian; } / ** * Konversi array byte dengan panjang 2 menjadi 16-bit int * * @param res * byte [] * @return int * * / public static int byte2int (byte [] res) {// res = inversionbyte (res); // Data byte digeser 24 bit kiri ke 0x ?? 000000, dan kemudian bergeser 8 bit tepat ke 0x00 ?? 00000 target int = (res [0] & 0xff) | ((res [1] << 8) & 0xff00); // | Menunjukkan target ambient atau pengembalian; }}Di atas adalah konten lengkap dari metode implementasi konversi array byte java dan int, panjang, pendek, byte yang dibawa kepada Anda. Saya harap semua orang akan mendukung wulin.com lebih banyak ~