日期:2014-05-20 浏览次数:21870 次
import java.util.Arrays;
public class ChangeBytesToNumber
{
public static void main(String[] args)
{
long l = -13912345678L;
byte[] b = numberToBytes(l); //数转成数组。
long number=bytesToNumber(b); //还原成数。
System.out.println("l is "+l);
System.out.println("number is "+number);
}
public static long bytesToNumber(byte[] b)
{
byte[] b1=Arrays.copyOfRange(b,0,8); //复制数值部分。
StringBuffer sb=new StringBuffer();
String tem=null;
int length=b1.length;
for(int i=0;i<length;i++) //组成16进制字符串。
{
tem=(b1[length-i-1]>=0)?Integer.toHexString(b1[length-i-1]):
Integer.toHexString(b1[length-i-1]).substring(6);
sb.append(tem);
}
long result=Long.parseLong(sb.toString(),16); //转成long型值。
result=(b[8]==0)?result:0-result; //处理符号。
return result;
}
public static byte[] numberToBytes(long l)
{
byte [] b = new byte[9];
b[8]=(l>0)?(byte)0:(byte)1; //l>0,b[8]=0;l<0,b[8]=1。
l=(l>0)?l:0-l; //处理l的绝对值。
b[0] = (byte) (l & 0x000000000000FFL);
b[1] = (byte) ((l & 0x0000000000FF00L) >> 8);
b[2] = (byte) ((l & 0x0000000000FF0000L) >> 16);
b[3] = (byte) ((l & 0x00000000FF000000L) >> 24);
b[4] = (byte) ((l & 0x000000FF00000000L) >> 32);
b[5] = (byte) ((l & 0x0000FF0000000000L) >> 40);
b[6] = (byte) ((l & 0x00FF000000000000L) >> 48);
b[7] = (byte) ((l & 0xFF00000000000000L) >> 56);
return b;
}
}
------解决方案--------------------
/**
* 各基础类型与byte之间的转换
* @author shanl
*
*/
public class Utility {
/**
* 将short转成byte[2]
* @param a
* @return
*/
public static byte[] short2Byte(short a){
byte[] b = new byte[2];
b[0] = (byte) (a >> 8);
b[1] = (byte) (a);
return b;
}
/**
* 将short转成byte[2]
* @param a
* @param b
* @param offset b中的偏移量
*/
public static void short2Byte(short a, byte[] b, int offset){
b[offset] = (byte) (a >> 8);
b[offset+1] = (byte) (a);
}
/**
* 将byte[2]转换成short
* @param b
* @return
*/
public static short byte2Short(byte[] b){
return (short) (((b[0] & 0xff) << 8) | (b[1] & 0xff));
}
/**
* 将byte[2]转换成short
* @param b
* @param offset
* @return
*/
public static short byte2Short(byte[] b, int offset){
return (short) (((b[offset] & 0xff) << 8) | (b[offset+1] & 0xff));
}
/**
* long转byte[8]
*
* @param a
* @param b
* @param offset
* b的偏移量
*/
public static void long2Byte(long a, byte[] b, int offset) {
b[offset + 0] = (byte) (a >> 56);
b[offset + 1] = (byte) (a >> 48);
b[offset + 2] = (byte) (a >> 40);
b[offset + 3] = (byte) (a >> 32);
b[offset + 4] = (byte) (a >> 24);
b[offset + 5] = (byte) (a >> 16);
b[offset + 6] = (byte) (a >> 8);
b[offset + 7] = (byte) (a);
}
/**
* byte[8]转long
*
* @param b
* @param offset
* b的偏移量
* @return
*/
public static long byte2Long(byte[] b, int offset) {
return ((((long) b[offset + 0] & 0xff) << 56)
| (((long) b[offset + 1] & 0xff) << 48)
| (((long) b[offset + 2] & 0xff) << 40)
| (((long) b[offset + 3] & 0xff) << 32)
| (((long) b[offset + 4] & 0xff) << 24)
| (((long) b[offset + 5] & 0xff) << 16)
| (((long) b[offset + 6] & 0xff) << 8)
| (((long) b[offset + 7] & 0xff) << 0));
}
/**
* byte[8]转long
*
* @param b
* @return
*/
public static long byte2Long(byte[] b) {
return
((b[0]&0xff)<<56)|
((b[1]&0xff)<<48)|
((b[2]&0xff)<<40)|
((b[3]&0xff)<<32)|
((b[4]&0xff)<<24)|
((b[5]&0xff)<<16)|
((b[6]&0xff)<<8)|
(b[7]&0xff);
}
/**
* long转byte[8]
*
* @param a
* @return
*/
public static byte[] long2Byte(long a) {
byte[] b = new byte[4 * 2];
b[0] = (byte) (a >> 56);
b[1] = (byte) (a >> 48);
b[2] = (byte) (a >> 40);
b[3] = (byte) (a >> 32);
b[4] = (byte) (a >> 24);
b[5] = (byte) (a >> 16);
b[6] = (byte) (a >> 8);
b[7] = (byte) (a >> 0);
return b;
}
/**
* byte数组转int
*
* @param b
* @return
*/
public static int byte2Int(byte[] b) {
return ((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16)
| ((b[2] & 0xff) << 8) | (b[3] & 0xff);
}
/**
* byte数组转int
*
* @param b
* @param offset
* @return
*/
public static int byte2Int(byte[] b, int offset) {
return ((b[offset++] & 0xff) << 24) | ((b[offset++] & 0xff) << 16)
| ((b[offset++] & 0xff) << 8) | (b[offset++] & 0xff);
}
/**
* int转byte数组
*
* @param a
* @return
*/
public static byte[] int2Byte(int a) {
byte[] b = new byte[4];
b[0] = (byte) (a >> 24);
b[1] = (byte) (a >> 16);
b[2] = (byte) (a >> 8);
b[3] = (byte) (a);
return b;
}
/**
* int转byte数组
*
* @param a
* @param b
* @param offset
* @return
*/
public static void int2Byte(int a, byte[] b, int offset) {
b[offset++] = (byte) (a >> 24);
b[offset++] = (byte) (a >> 16);
b[offset++] = (byte) (a >> 8);
b[offset++] = (byte) (a);
}
}