ตัวอย่างมีดังนี้:
คลาสสาธารณะ DataTypeChangeHelper { / ** * แปลงไบต์ไบต์เดียวเป็น int 32 บิต * * @param b * byte * @return แปลงผลลัพธ์ * / public int unsignedbyTetoint (byte b) {return (int) b & 0xff; } / ** * แปลงไบต์ไบต์เดียวเป็นเลขฐานสิบหก * * @param b * byte * @return แปลงผลลัพธ์ * / สตริงคงที่สาธารณะ bytetohex (ไบต์ b) {int i = b & 0xff; return integer.tohexstring (i); } / ** * แปลงอาร์เรย์ของ 4bytes เป็น int 32 บิต * * @param buf * บัฟเฟอร์ไบต์ * ตำแหน่งใน @param byte [] ที่การแปลงเริ่มต้น * @return แปลงผลลัพธ์ * / สาธารณะคงที่ Long unsigned4ByTestoint (byte [] buf, pos) int secondByte = 0; int thichbyte = 0; int fourthbyte = 0; INT ดัชนี = POS; firstByte = (0x000000ff & ((int) buf [ดัชนี])); secondByte = (0x000000ff & ((int) buf [ดัชนี + 1])); ThirdByte = (0x000000ff & ((int) buf [ดัชนี + 2])); FourthByte = (0x000000ff & ((int) buf [ดัชนี + 3])); ดัชนี = ดัชนี + 4; return ((ยาว) (FirstByte << 24 | SecondByte << 16 | ThirdByte << 8 | FourthByte)) & 0xffffl; } / ** * แปลงสั้น 16 บิตเป็นอาร์เรย์ไบต์ * * @param s * สั้น * @return byte [] ความยาวคือ 2 * * / ไบต์คงที่สาธารณะ [] shorttobytearray (สั้น s) {byte [] เป้าหมาย = นิวไบต์ [2]; สำหรับ (int i = 0; i <2; i ++) {int offset = (targets.length - 1 - i) * 8; เป้าหมาย [i] = (ไบต์) ((s >>> ออฟเซ็ต) & 0xff); } คืนเป้าหมาย; } / ** * แปลงจำนวนเต็ม 32 บิตเป็นอาร์เรย์ไบต์ที่มีความยาว 4 * * @param s * int * @return byte [] * * / ไบต์คงที่สาธารณะ [] inttobytearray (int s) {byte [] เป้าหมาย = นิวไบต์ [2]; สำหรับ (int i = 0; i <4; i ++) {int offset = (targets.length - 1 - i) * 8; เป้าหมาย [i] = (ไบต์) ((s >>> ออฟเซ็ต) & 0xff); } คืนเป้าหมาย; } / ** * ยาวถึงไบต์ [] * * @param s * ยาว * @return byte [] * * / ไบต์คงที่สาธารณะ [] longtobytearray (ยาว s) {byte [] เป้าหมาย = นิวไบต์ [2]; สำหรับ (int i = 0; i <8; i ++) {int offset = (targets.length - 1 - i) * 8; เป้าหมาย [i] = (ไบต์) ((s >>> ออฟเซ็ต) & 0xff); } คืนเป้าหมาย; } / ** int 32 บิตถึงไบต์ []* / ไบต์คงที่สาธารณะ [] int2Byte (int res) {byte [] เป้าหมาย = byte ใหม่ [4]; เป้าหมาย [0] = (ไบต์) (res & 0xff); // เป้าหมายหลักต่ำสุด [1] = (byte) ((res >> 8) & 0xff); // เป้าหมายหลักต่ำที่สอง [2] = (ไบต์) (res >> 16) & 0xff); คืนเป้าหมาย; } / ** * แปลงอาร์เรย์ไบต์ที่มีความยาว 2 เป็น int 16 บิต * * @param res * byte [] * @return int * * / สาธารณะคงที่ int byte2int (byte [] res) {// res = inversionByte (res); // ข้อมูลไบต์ถูกเลื่อน 24 บิตเหลือเป็น 0x ?? 000000 จากนั้นเลื่อน 8 บิตไปเป็น 0x00 ?? 00000 int เป้าหมาย = (res [0] & 0xff) | ((res [1] << 8) & 0xff00); - ระบุเป้าหมายโดยรอบหรือส่งคืน -ข้างต้นเป็นเนื้อหาเต็มรูปแบบของวิธีการใช้งานการแปลงของอาร์เรย์ Java Byte และ Int, Long, Short, Byte ที่มาถึงคุณ ฉันหวังว่าทุกคนจะสนับสนุน wulin.com เพิ่มเติม ~