public class BitUtil
{
private static String HexCode[] = {"0", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "a", "b", "c", "d", "e", "f"};
public static void intToBytes(int i, byte[] buf, int offset)
{
buf[offset + 3] = (byte) (0xff & i);
buf[offset + 2] = (byte) ((0xff00 & i) >> 8);
buf[offset + 1] = (byte) ((0xff0000 & i) >> 16);
buf[offset + 0] = (byte) ((0xff000000 & i) >> 24);
}
public static int bytesToInt(byte abyte0[], int offset)
{
return (0xff & abyte0[offset + 0]) << 24
| (0xff & abyte0[offset + 1]) << 16
| (0xff & abyte0[offset + 2]) << 8 | 0xff & abyte0[offset + 3];
}
public static void longToByte(long i, byte[] b, int offset)
{
b[offset + 7] = (byte) (i & 0x00000000000000ff);
b[offset + 6] = (byte) ((i >> 8) & 0x00000000000000ff);
b[offset + 5] = (byte) ((i >> 16) & 0x00000000000000ff);
b[offset + 4] = (byte) ((i >> 24) & 0x00000000000000ff);
b[offset + 3] = (byte) ((i >> 32) & 0x00000000000000ff);
b[offset + 2] = (byte) ((i >> 40) & 0x00000000000000ff);
b[offset + 1] = (byte) ((i >> 48) & 0x00000000000000ff);
b[offset + 0] = (byte) ((i >> 56) & 0x00000000000000ff);
}
public static long byteToLong(byte[] bs, int offset)
{
long i1 = (long) (bs[offset + 7] & 0x00ff);
long i2 = (long) (bs[offset + 6] & 0x00ff) << 8;
long i3 = (long) (bs[offset + 5] & 0x00ff) << 16;
long i4 = (long) (bs[offset + 4] & 0x00ff) << 24;
long i5 = (long) (bs[offset + 3] & 0x00ff) << 32;
long i6 = (long) (bs[offset + 2] & 0x00ff) << 40;
long i7 = (long) (bs[offset + 1] & 0x00ff) << 48;
long i8 = (long) (bs[offset + 0] & 0x00ff) << 56;
return i1 | i2 | i3 | i4 | i5 | i6 | i7 | i8;
}
public static void bytesCopy(byte abyte0[], byte abyte1[], int i, int j,
int k)
{
int i1 = 0;
for (int l = i; l < abyte0.length && l <= j; l++)
{
abyte1[k + i1] = abyte0[l];
i1++;
}
}
public static String byte2String(byte buf[], int offset, int len)
{
byte[] b = new byte[len];
System.arraycopy(buf, offset, b, 0, len);
return new String(b).trim();
}
public static void stringToByte(String str, byte buf[], int offset, int len)
{
byte[] b = str.getBytes();
System.arraycopy(b, 0, buf, offset, b.length);
}
public static int ocsToByte(String str, byte buf[], int offset)
{
if (str == null || str.length() == 0)
{
buf[offset] = 0;
return offset + 1;
}
else
{
byte[] b = str.getBytes();
System.arraycopy(b, 0, buf, offset, b.length);
buf[offset + b.length] = 0;
return offset + b.length + 1;
}
}
public static String byteToOcs(byte[] buf, int offset)
{
int i;
for (i = offset; i < buf.length; i++)
{
if (buf[i] == 0)
{
break;
}
}
if (i == offset)
{
return null;
}
else
{
byte[] b = new byte[i - offset];
System.arraycopy(buf, offset, b, 0, b.length);
return new String(b);
}
}
public static int ocsLenth(String str)
{
if (str == null)
{
return 1;
}
else
{
return str.getBytes().length + 1;
}
}
public static String byteToHexString(byte b)
{
int n = b;
if (n < 0)
{
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return HexCode[d1] + HexCode[d2];
}
public static String byteArrayToHexString(byte b[])
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < b.length; i++)
{
buf.append(byteToHexString(b[i]));
}
String result = buf.toString();
return result;
}
public static byte[] hexString2byte(String hexString)
{
if (hexString == null || hexString.length() % 2 != 0)
{
return null;
}
byte[] result = new byte[hexString.length() / 2];
for (int i = 0; i < hexString.length(); i += 2)
{
result[i / 2] = (byte) (Integer.parseInt(
hexString.substring(i, i + 2), 16) & 0xff);
}
return result;
}
}