Java基本类型与byte数组之间相互转换:
package cn.teaey.utils;
import java.nio.charset.Charset;
public class ByteUtil
{
public static byte[] getBytes(short data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data & 0xff);
bytes1 = (byte) ((data & 0xff00) >> 8);
return bytes;
}
public static byte[] getBytes(char data)
{
byte[] bytes = new byte[2];
bytes[0] = (byte) (data);
bytes[1] = (byte) (data >> 8);
return bytes;
}
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;
}
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;
}
public static byte[] getBytes(float data)
{
int intBits = Float.floatToIntBits(data);
return getBytes(intBits);
}
public static byte[] getBytes(double data)
{
long intBits = Double.doubleToLongBits(data);
return getBytes(intBits);
}
public static byte[] getBytes(String data, String charsetName)
{
Charset charset = Charset.forName(charsetName);
return data.getBytes(charset);
}
public static byte[] getBytes(String data)
{
return getBytes(data, "GBK");
}
public static short getShort(byte[] bytes)
{
return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] <<8)));
}
public static char getChar(byte[] bytes)
{
return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] <<8)));
}
public static int getInt(byte[] bytes)
{
return (0xff & bytes[0]) | (0xff00 & (bytes[1] <<8)) | (0xff0000 & (bytes[2] <<16)) | (0xff000000 & (bytes[3] <<24));
}
public static long getLong(byte[] bytes)
{
return(0xffL & (long)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));
}
public static float getFloat(byte[] bytes)
{
return Float.intBitsToFloat(getInt(bytes));
}
public static double getDouble(byte[] bytes)
{
long l &#61; getLong(bytes);
System.out.println(l);
return Double.longBitsToDouble(l);
}
public static String getString(byte[] bytes, String charsetName)
{
return new String(bytes, Charset.forName(charsetName));
}
public static String getString(byte[] bytes)
{
return getString(bytes, "GBK");
}
public static void main(String[] args)
{
short s &#61; 122;
int i &#61; 122;
long l &#61; 1222222;
char c &#61; &#39;a&#39;;
float f &#61; 122.22f;
double d &#61; 122.22;
String string &#61; "我是好孩子";
System.out.println(s);
System.out.println(i);
System.out.println(l);
System.out.println(c);
System.out.println(f);
System.out.println(d);
System.out.println(string);
System.out.println("**************");
System.out.println(getShort(getBytes(s)));
System.out.println(getInt(getBytes(i)));
System.out.println(getLong(getBytes(l)));
System.out.println(getChar(getBytes(c)));
System.out.println(getFloat(getBytes(f)));
System.out.println(getDouble(getBytes(d)));
System.out.println(getString(getBytes(string)));
}
}
java的基本类型存储长度都是固定的&#xff0c;不因机器的不同而不同&#xff0c;因此使java拥有了良好的移植性。
最近在做项目时&#xff0c;需要在一个变量里存放16位长的二进制数字&#xff0c;查看上表可知short类型正好符合需要。
但是因为java中的数字类型都是有符号的&#xff0c;因此short类型的第一位被用于表示符号&#xff0c;实际存储长度只有15位&#xff0c;
即&#xff0d;7FFF&#xff0d;&#xff0b;7FFF。那如果要存放&#xff0b;7FFF&#xff0d;&#xff0b;FFFF的数字该怎么办呢&#xff1f;只有采取变通的办法&#xff0c;采用变换算法。我们可以利用&#xff0d;7FFF&#xff0d;&#xff0d;0001来存放大于7FFF的数字。转换公式为&#xff0d;(FFFF&#xff0d;X)&#xff0d;1。由于有了符号&#xff0c;所以&#xff0b;0000和&#xff0d;0000是相等的&#xff0c;所以比无符号数少了1个数字&#xff0c;所以要在公式里减1&#xff0c;由于这个问题&#xff0c;用这个方法我们就不能存放&#xff0b;8000了。
用这个方法可以基本满足我们的需要&#xff0c;如有特殊需要&#xff0c;如要存储&#xff0b;8000&#xff0c;那只有采取别的办法了。