我们如果多了解一些底层东西,对开发程序是很有帮助的.
public class DESUtil {
/**
* byte[] to short
* @param b
* @param index
* @return short
*/
public static short getShort(byte[] b, int index) throws Exception{
return (short) (((b[index] << 8) | b[index + 1] & 0xff));
}
public static void putShort(byte b[], short s, int index) throws Exception{
b[index] = (byte) (s >> 8);
b[index + 1] = (byte) (s >> 0);
}
/**
* byte[] to float
* @param b
* @param index
* @return float
*/
public static float getFloat(byte[] b,int index) throws Exception{
int i = 0;
i = ((((b[index + 3]&0xff)<<8 | (b[index + 2]&0xff))<<8) | (b[index + 1]&0xff))<<8 | (b[index + 0]&0xff);
return Float.intBitsToFloat(i);
}
public static void putFloat(byte[] bb, float f ,int index) throws Exception{
int l = Float.floatToIntBits(f);
for(int i = 0; i < 4; i++ ){
bb[index + i]= new Integer(l).byteValue();
l=l>>8;
}
}
/**
* byte[] to long
* @param b
* @param index
* @return long
*/
public static long getLong(byte[] b, int index) throws Exception{
return ((((long) b[index + 0] & 0xff) << 56)
| (((long) b[index + 1] & 0xff) << 48)
| (((long) b[index + 2] & 0xff) << 40)
| (((long) b[index + 3] & 0xff) << 32)
| (((long) b[index + 4] & 0xff) << 24)
| (((long) b[index + 5] & 0xff) << 16)
| (((long) b[index + 6] & 0xff) << 8) | (((long) b[index + 7] & 0xff) << 0));
}
public static void putLong(byte[] bb, long x, int index) throws Exception{
bb[index + 0] = (byte) (x >> 56);
bb[index + 1] = (byte) (x >> 48);
bb[index + 2] = (byte) (x >> 40);
bb[index + 3] = (byte) (x >> 32);
bb[index + 4] = (byte) (x >> 24);
bb[index + 5] = (byte) (x >> 16);
bb[index + 6] = (byte) (x >> 8);
bb[index + 7] = (byte) (x >> 0);
}
/**
* byte[] to double
* @param b
* @param index
* @return double
*/
public static double getDouble(byte[] b,int index){
long l;
l=b[index + 0];
l&=0xff;
l|=((long)b[index + 1]<<8);
l&=0xffff;
l|=((long)b[index + 2]<<16);
l&=0xffffff;
l|=((long)b[index + 3]<<24);
l&=0xffffffffl;
l|=((long)b[index + 4]<<32);
l&=0xffffffffffl;
l|=((long)b[index + 5]<<40);
l&=0xffffffffffffl;
l|=((long)b[index + 6]<<48);
l&=0xffffffffffffffl;
l|=((long)b[index + 7]<<56);
return Double.longBitsToDouble(l);
}
/**
* byte[] to double
* @param b
* @param index
* @return double
*/
public static void putDouble(byte[] bb, Double d , int index) throws Exception{
long l = Double.doubleToLongBits(d);
for(int i = 0; i < 8; i++ ){
bb[index + i]= new Long(l).byteValue();
l=l>>8;
}
}
/**
* byte[] to Integer
* @param b
* @param index
* @return Integer
*/
public static int getInt(byte[] b, int index) throws Exception{
return (int) ((((b[index + 0] & 0xff) << 24)
| ((b[index + 1] & 0xff) << 16)
| ((b[index + 2] & 0xff) << 8) | ((b[index + 3] & 0xff) << 0)));
}
public static void putInt(byte[] bb, int x, int index) throws Exception{
bb[index + 0] = (byte) (x >> 24);
bb[index + 1] = (byte) (x >> 16);
bb[index + 2] = (byte) (x >> 8);
bb[index + 3] = (byte) (x >> 0);
}
public static String getString(byte[] b ,int index,int size) throws Exception{
byte[] string = new byte[size];
for(int i = 0;i < string.length ; i++){
string[i] = b[index + i];
}
return new String(string,"UTF-8");
}
public static void putString(byte[] bb, String s,int index) throws Exception{
byte[] string = s.getBytes("utf-8");
for(int i= 0 ;i < string.length ; i++)
bb[index + i] = string[i];
}
public static byte getByte(byte[] b,int index) throws Exception{
return b[index];
}
public static void putByte(byte[] bb,byte b,int index) throws Exception{
bb[index] = b;
}
public static byte[] getBytes(byte[] b,int index,int size) throws Exception{
byte[] return_byte = new byte[size];
for(int i = 0;i< size ;i++){
return_byte[i] = b[index + i];
}
return return_byte;
}
public static void putBytes(byte[] bb, byte[] putbb ,int index) throws Exception{
int length = putbb.length;
for(int i = 0;i< length ;i ++){
bb[index + i] = putbb[i];
}
}
}