1 24 25 package com.mckoi.util; 26 27 32 33 public class ByteArrayUtil { 34 35 38 public static final char getChar(byte[] arr, int offset) { 39 int c1 = (((int) arr[offset + 0]) & 0x0FF); 40 int c2 = (((int) arr[offset + 1]) & 0x0FF); 41 return (char) ((c1 << 8) + (c2)); 42 } 43 44 47 public static final void setChar(char value, byte[] arr, int offset) { 48 arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF); 49 arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF); 50 } 51 52 55 public static final short getShort(byte[] arr, int offset) { 56 int c1 = (((int) arr[offset + 0]) & 0x0FF); 57 int c2 = (((int) arr[offset + 1]) & 0x0FF); 58 return (short) ((c1 << 8) + (c2)); 59 } 60 61 64 public static final void setShort(short value, byte[] arr, int offset) { 65 arr[offset + 0] = (byte) ((value >>> 8) & 0x0FF); 66 arr[offset + 1] = (byte) ((value >>> 0) & 0x0FF); 67 } 68 69 72 public static final int getInt(byte[] arr, int offset) { 73 int c1 = (((int) arr[offset + 0]) & 0x0FF); 74 int c2 = (((int) arr[offset + 1]) & 0x0FF); 75 int c3 = (((int) arr[offset + 2]) & 0x0FF); 76 int c4 = (((int) arr[offset + 3]) & 0x0FF); 77 return (c1 << 24) + (c2 << 16) + (c3 << 8) + (c4); 78 } 79 80 83 public static final void setInt(int value, byte[] arr, int offset) { 84 arr[offset + 0] = (byte) ((value >>> 24) & 0xFF); 85 arr[offset + 1] = (byte) ((value >>> 16) & 0xFF); 86 arr[offset + 2] = (byte) ((value >>> 8) & 0xFF); 87 arr[offset + 3] = (byte) ((value >>> 0) & 0xFF); 88 } 89 90 93 public static final long getLong(byte[] arr, int offset) { 94 long c1 = (((int) arr[offset + 0]) & 0x0FF); 95 long c2 = (((int) arr[offset + 1]) & 0x0FF); 96 long c3 = (((int) arr[offset + 2]) & 0x0FF); 97 long c4 = (((int) arr[offset + 3]) & 0x0FF); 98 long c5 = (((int) arr[offset + 4]) & 0x0FF); 99 long c6 = (((int) arr[offset + 5]) & 0x0FF); 100 long c7 = (((int) arr[offset + 6]) & 0x0FF); 101 long c8 = (((int) arr[offset + 7]) & 0x0FF); 102 103 return (c1 << 56) + (c2 << 48) + (c3 << 40) + 104 (c4 << 32) + (c5 << 24) + (c6 << 16) + (c7 << 8) + (c8); 105 } 106 107 110 public static final void setLong(long value, byte[] arr, int offset) { 111 arr[offset + 0] = (byte) ((value >>> 56) & 0xFF); 112 arr[offset + 1] = (byte) ((value >>> 48) & 0xFF); 113 arr[offset + 2] = (byte) ((value >>> 40) & 0xFF); 114 arr[offset + 3] = (byte) ((value >>> 32) & 0xFF); 115 arr[offset + 4] = (byte) ((value >>> 24) & 0xFF); 116 arr[offset + 5] = (byte) ((value >>> 16) & 0xFF); 117 arr[offset + 6] = (byte) ((value >>> 8) & 0xFF); 118 arr[offset + 7] = (byte) ((value >>> 0) & 0xFF); 119 } 120 121 122 } 123 | Popular Tags |