short、int、long与byte之间的转换工具类

转自:http://zuoqiang.iteye.com/blog/1545549

/**
 * 各基础类型与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);
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值