short、int、long与byte之间的转换工具类
来源:互联网 发布:java string 添加空格 编辑:程序博客网 时间:2024/05/19 21:03
/**
* 各基础类型与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);
}
}
* 各基础类型与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
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short、int、long与byte之间的转换工具类
- short,int,long与byte数组之间的转换
- 【Java】short、int、float、boolean与byte之间的转换工具类
- long、int和short与byte之间的转化
- Java异常处理及异常机制介绍 && long,int,short与byte数组之间的转换
- short,int,long,double,string,float与byte数组之间的转换
- Java 语言中 short 与 byte[]之间的转换,int、long 类似。
- java中long,int,short与byte数组之间的转换
- java中long,int,short与byte数组之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中byte数组与int,long,short,float,char之间的转换
- java中long,int,short与byte数组之间的转换
- Java中byte数组与short,int,char,long,float,double之间的转换
- java中long,int,short与byte数组之间的转换
- linux 查看进程和终止进程
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- (1.2.1.2)单链表:有序链表合并、交集、差集
- 指针和数组的区别
- iotop实时监控磁盘
- short、int、long与byte之间的转换工具类
- Count and Say
- VS2013 密钥
- 2015/03/13开始模式识别与机器学习之旅
- Domain = FBSOpenApplicationErrorDomain, Code = 4
- ipTraf命令行网络工具
- eclipse集成html5插件
- ActiveMQ初体验
- Android视图绘制流程完全解析,带你一步步深入了解View(二)