package cn.ly.tool;
/**
* Little Endian not Big Endian
* 0X12345678
* <Pre> <B>Little Endian</B>: 78 56 34 12
* <B>Big Endian</B>: 12 34 56 78
* </Pre>
* @author ly 810497097@qq.com
*
*/
public class ByteUtil {
/**
* @param src
* @param dest
* @param offset [offset, offset + 2) or [offset, offset + 1]
*/
public static void charToByteArray(char src, byte[] dest, int offset) {
dest[offset + 0] = (byte)((src >> 0) & 0XFF);
dest[offset + 1] = (byte)((src >> 8) & 0XFF);
}
/**
*
* @param src
* @param offset
* @return
*/
public static char byteArrayToChar(byte[] src, int offset) {
return (char)
(
((src[offset + 0] & 0XFF) << 0) |
((src[offset + 1] & 0XFF) << 8)
);
}
/**
*
* @param src
* @param dest
* @param offset
*/
public static void shortToByteArray(short src, byte[] dest, int offset) {
dest[offset + 0] = (byte)((src >> 0) & 0XFF);
dest[offset + 1] = (byte)((src >> 8) & 0XFF);
}
/**
*
* @param src
* @param offset
* @return
*/
public static short byteArrayToShort(byte[] src, int offset) {
return (short)
(
((src[offset + 0] & 0XFF) << 0) |
((src[offset + 1] & 0XFF) << 8)
);
}
/**
*
* @param src
* @param dest
* @param offset
*/
public static void intToByteArray(int src, byte[] dest, int offset) {
dest[offset + 0] = (byte) ((src >> 0) & 0XFF);// 最低
dest[offset + 1] = (byte) ((src >> 8) & 0XFF);// 次低
dest[offset + 2] = (byte) ((src >> 16) & 0XFF);// 次高
dest[offset + 3] = (byte) ((src >> 24) & 0XFF);// 最高
}
/**
*
* @param src
* @param offset
* @return
*/
public static int byteArrayToInt(byte[] src, int offset) {
return (int)
(
((src[offset + 0] & 0XFF) << 0) |
((src[offset + 1] & 0XFF) << 8) |
((src[offset + 2] & 0XFF) << 16) |
((src[offset + 3] & 0XFF) << 24)
);
}
/**
*
* @param src
* @param dest
* @param offset
*/
public static void floatToByteArray(float src, byte[] dest, int offset) {
int srcInt = Float.floatToIntBits(src);
dest[offset + 0] = (byte) ((srcInt >> 0) & 0XFF);
dest[offset + 1] = (byte) ((srcInt >> 8) & 0XFF);
dest[offset + 2] = (byte) ((srcInt >> 16) & 0XFF);
dest[offset + 3] = (byte) ((srcInt >> 24) & 0XFF);
}
/**
*
* @param src
* @param offset
* @return
*/
public static float byteArrayToFloat(byte[] src, int offset) {
return Float.intBitsToFloat(
((src[offset + 0] & 0XFF) << 0) |
((src[offset + 1] & 0XFF) << 8) |
((src[offset + 2] & 0XFF) << 16) |
((src[offset + 3] & 0XFF) << 24)
);
}
/**
*
* @param src
* @param dest
* @param offset
*/
public static void doubleToByteArray(double src, byte[] dest, int offset) {
long srcLong = Double.doubleToLongBits(src);
dest[offset + 0] = (byte) ((srcLong >> 0) & 0XFF);
dest[offset + 1] = (byte) ((srcLong >> 8) & 0XFF);
dest[offset + 2] = (byte) ((srcLong >> 16) & 0XFF);
dest[offset + 3] = (byte) ((srcLong >> 24) & 0XFF);
dest[offset + 4] = (byte) ((srcLong >> 32) & 0XFF);
dest[offset + 5] = (byte) ((srcLong >> 40) & 0XFF);
dest[offset + 6] = (byte) ((srcLong >> 48) & 0XFF);
dest[offset + 7] = (byte) ((srcLong >> 56) & 0XFF);
}
/**
*
* @param src
* @param offset
* @return
*/
public static double byteArrayToDouble(byte[] src, int offset) {
return Double.longBitsToDouble(
((src[offset + 0] & 0XFFL) << 0) |
((src[offset + 1] & 0XFFL) << 8) |
((src[offset + 2] & 0XFFL) << 16) |
((src[offset + 3] & 0XFFL) << 24) |
((src[offset + 4] & 0XFFL) << 32) |
((src[offset + 5] & 0XFFL) << 40) |
((src[offset + 6] & 0XFFL) << 48) |
((src[offset + 7] & 0XFFL) << 56)
);
}
/**
* Returns the <a href="http://en.wikipedia.org/wiki/IEEE_754-1985">IEEE 754</a>
* @param src
* @param offset
* @return
*/
public static double byteArrayToDoubleAuto(byte[] src, int offset, int byteSize) {
long srcLong = 0;
for( int i = 0; i < byteSize; ++i) {
srcLong |= ((src[offset + i] & 0XFFL) << 8 * i);
}
return Double.longBitsToDouble(srcLong);
}
}
// how to fix
public static void basicTypeToByteArray(Number src, byte[] dest, int offset) {
if( dest == null) {
throw new NullPointerException("dest == null");
}
final int size;
if( src instanceof Short) {
size = Short.SIZE;
} else if( src instanceof Integer) {
size = Integer.SIZE;
} else if( src instanceof Float) {
size = Float.SIZE;
} else if( src instanceof Double) {
size = Double.SIZE;
} else if( src instanceof Long) {
size = Long.SIZE;
} else {
throw new UnknownError("the convertion don't contain the type:" + src.getClass().getName());
}
if( dest.length - offset < size) {
throw new IndexOutOfBoundsException("need number is beyond the array's bound!"
+ " length:" + dest.length + " offset:" + offset + " desired size value:" + size);
}
for( int i = 0; i < size; ++i) {
//dest[offset + i] = (byte)((src >> (8 * i)) & 0XFF);
}
}