java 处理.net发送过来的数组,数组转换基本类型,涉及到高低位转换:
/**
* short-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(short data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
return bytes;
}
/**
* char-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(char data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data);
bytes[1] = (byte) (data >> 8);
return bytes;
}
/**
* int-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(int data)
{
byte[] bytes = new byte[4];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data & 0xff00) >> 8);
bytes[2] = (byte) ((data & 0xff0000) >> 16);
bytes[3] = (byte) ((data & 0xff000000) >> 24);
return bytes;
}
/**
* long-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(long data)
{
byte[] bytes = new byte[8];
bytes[0] = (byte) (data & 0xff);
bytes[1] = (byte) ((data >> 8) & 0xff);
bytes[2] = (byte) ((data >> 16) & 0xff);
bytes[3] = (byte) ((data >> 24) & 0xff);
bytes[4] = (byte) ((data >> 32) & 0xff);
bytes[5] = (byte) ((data >> 40) & 0xff);
bytes[6] = (byte) ((data >> 48) & 0xff);
bytes[7] = (byte) ((data >> 56) & 0xff);
return bytes;
}
/**
* float-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(float data)
{
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}
/**
* double-->byte[]
* @param data
* @return
*/
public static byte[] getBytes(double data)
{
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}
/**
* string-->byte[] 按字符集
* @param data
* @return
*/
public static byte[] getBytes(String data, String charsetName)
{
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
/**
* string-->byte[] utf-8
* @param data
* @return
*/
public static byte[] getBytes(String data)
{
return getBytes(data, "utf-8");
}
/**
* byte[]-->short
* @param data
* @return
*/
public static short getShort(byte[] bytes)
{
return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
* byte[]-->short
* @param data
* @return
*/
public static char getChar(byte[] bytes)
{
return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
}
/**
* byte[]-->int
* @param data
* @return
*/
public static int getInt(byte[] bytes)
{
return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));
}
/**
* byte[]-->long
* @param data
* @return
*/
public static long getLong(byte[] bytes)
{
return(0xffL & bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))
| (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));
}
/**
* byte[]-->float
* @param data
* @return
*/
public static float getFloat(byte[] bytes)
{
return Float.intBitsToFloat(getInt(bytes));
}
/**
* byte[]-->double
* @param data
* @return
*/
public static double getDouble(byte[] bytes)
{
long l = getLong(bytes);
System.out.println(l);
return Double.longBitsToDouble(l);
}
/**
* byte[]-->String 按字符集
* @param data
* @return
*/
public static String getString(byte[] bytes, String charsetName)
{
return new String(bytes, Charset.forName(charsetName));
}
/**
* byte[]-->String
* @param data
* @return
*/
public static String getString(byte[] bytes)
{
return getString(bytes, "utf-8");
}