INT BYTE4
来源:互联网 发布:淘宝同行恶意投诉售假 编辑:程序博客网 时间:2024/06/05 23:59
这里简单记录下两种转换方式:
转载
第一种:
1、int与byte[]之间的转换(类似的byte short,long型)
- /**
- * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用
- * @param value
- * 要转换的int值
- * @return byte数组
- */
- public static byte[] intToBytes( int value )
- {
- byte[] src = new byte[4];
- src[3] = (byte) ((value>>24) & 0xFF);
- src[2] = (byte) ((value>>16) & 0xFF);
- src[1] = (byte) ((value>>8) & 0xFF);
- src[0] = (byte) (value & 0xFF);
- return src;
- }
- /**
- * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。 和bytesToInt2()配套使用
- */
- public static byte[] intToBytes2(int value)
- {
- byte[] src = new byte[4];
- src[0] = (byte) ((value>>24) & 0xFF);
- src[1] = (byte) ((value>>16)& 0xFF);
- src[2] = (byte) ((value>>8)&0xFF);
- src[3] = (byte) (value & 0xFF);
- return src;
- }
byte[]转int
- /**
- * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
- *
- * @param src
- * byte数组
- * @param offset
- * 从数组的第offset位开始
- * @return int数值
- */
- public static int bytesToInt(byte[] src, int offset) {
- int value;
- value = (int) ((src[offset] & 0xFF)
- | ((src[offset+1] & 0xFF)<<8)
- | ((src[offset+2] & 0xFF)<<16)
- | ((src[offset+3] & 0xFF)<<24));
- return value;
- }
- /**
- * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
- */
- public static int bytesToInt2(byte[] src, int offset) {
- int value;
- value = (int) ( ((src[offset] & 0xFF)<<24)
- |((src[offset+1] & 0xFF)<<16)
- |((src[offset+2] & 0xFF)<<8)
- |(src[offset+3] & 0xFF));
- return value;
- }
第二种:
1、int与byte[]之间的转换(类似的byte short,long型)
- /**
- * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。
- * @param value
- * 要转换的int值
- * @return byte数组
- */
- public static byte[] intToBytes(int value)
- {
- byte[] byte_src = new byte[4];
- byte_src[3] = (byte) ((value & 0xFF000000)>>24);
- byte_src[2] = (byte) ((value & 0x00FF0000)>>16);
- byte_src[1] = (byte) ((value & 0x0000FF00)>>8);
- byte_src[0] = (byte) ((value & 0x000000FF));
- return byte_src;
- }
byte[]转int
- /**
- * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。
- *
- * @param ary
- * byte数组
- * @param offset
- * 从数组的第offset位开始
- * @return int数值
- */
- public static int bytesToInt(byte[] ary, int offset) {
- int value;
- value = (int) ((ary[offset]&0xFF)
- | ((ary[offset+1]<<8) & 0xFF00)
- | ((ary[offset+2]<<16)& 0xFF0000)
- | ((ary[offset+3]<<24) & 0xFF000000));
- return value;
- }
0 0
- INT BYTE4
- int
- int?
- int ?
- int
- Int
- int?
- int*&、int&*
- int*&、int&*
- Int (*(*F)(int,int))(int)
- int (*func(int)) (int *,int)
- int; int*; int&区别
- int (*a)(int) ; int (*a[10])(int); int *(*a)(int) ;
- typedef int (* func)(int ,int )
- (int) &、(int)、(int &)的区别
- typedef int (* func)(int ,int )
- pair<int, int> set<pair<int, int>>
- int socket(int,int,int)详解
- Retrofit的使用与深入学习(下)
- Java中int转String 和 String转int 各方法效率对比
- Linux-16、17、18-服务器操作系统CentOS6.5安装实战(L003-30,31,32)
- Swift学习笔记二:常用类型的注意事项
- Android http操作
- INT BYTE4
- Block的使用
- Ubuntu下Spark开发环境搭建
- java获取当前类的绝对路径
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- 使用意图跳转到桌面
- Android6.0系统权限那些事
- Unity工程中 .Meta 文件的来龙去脉
- getDeclaredMethod()和getMethod()的区别