short、int、long与byte之间的转换工具类
来源:互联网 发布:手机测评软件 编辑:程序博客网 时间:2024/05/19 21:02
/** * 各基础类型与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数组之间的转换
- 根据浏览器语言,页面动态加载对应的js文件
- scala常见问题总结(IDE:idea)
- mybaits(1)_第一个mybaits项目
- HDU 1869 六度分离
- *** FATAL ERROR L250: CODE SIZE LIMIT IN RESTRICTED VERSION EXCEEDED
- short、int、long与byte之间的转换工具类
- Jquery中attr()和prop()的区别
- 小菜学习日记—Java中new Date()获取当前时间
- hibernate 常见面试题
- 【字符串】【kmp模板】
- DataFrame改变列类型
- Android异步加载
- Java类中加载Spring中的ApplicationContext.xml文件的方式
- JEECG中关于Excel表格的