byte[]数组和int之间的转换
这里简单记录下两种转换方式:
第一种:
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;
- }
byte[]数组和int之间的转换的更多相关文章
- 【转】java中byte数组与int类型的转换(两种方式)----不错
原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...
- byte数组和int之间相互转化的方法
Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...
- 深入 JAVA里面关于byte数组和String之间的转换问题
把byte转化成string,必须经过编码. 例如下面一个例子: importjava.io.UnsupportedEncodingException; publicclass test{ pub ...
- Byte数组和Int的互相转换
public static int bytesToInt(byte[] bytes) { int addr = bytes[0] & 0xFF; addr |= ((bytes[1] < ...
- java中byte数组与int类型的转换(两种方式)
http://blog.csdn.net/z69183787/article/details/38564219 http://blog.csdn.net/z69183787/article/detai ...
- Java 中 byte、byte 数组和 int、long 之间的转换
Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...
- java中byte,byte[]和int之间的转换
1>byte类型转换为,直接隐式转换,适用于要求保持数值不变,例如要求进行数值计算 如 byte b=0x01; int i=b; 2>另一种是要求保持最低字节中各个位不变,3个高字节全部 ...
- byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化
我们先来看看byte bool int ushort 等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...
- 如何实现数组和 List 之间的转换?(未完成)
如何实现数组和 List 之间的转换?(未完成)
随机推荐
- RP2837 IN1-IN2 对应关系 2路DI
RP2837 IN1-IN2 对应关系: IN1 ARM-IO2 PA16 IN2 ARM-IO6 PA4 root@sama5d3-linux:~ echo 16 > /sy ...
- Office Web App2013 在线查看PDF文件
经常会有客户问,在SharePoint中,如何在浏览器中查看与编辑文档,通常给出的解决方案是集成Office Web App. 而在实际应用过程中,客户通常会要求实现PDF文件在线查看,对于PDF文件 ...
- Linux系统查看公网IP地址
curl members.3322.org/dyndns/getip
- Linux快速定位并且杀掉占用端口的进程
1.定位 lsof -i:8811(端口号) 2.杀掉进程 kill -9 63924
- python ascii codec can't decode
提示错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 240: ordinal not in range ...
- 关于Cocos2d-x中自定义的调用注意事项
1.在实例类Student.h中定义一个自己的方法 public: int getSno(); 2.在实例类Student.cpp中实现这个方法 int Student::getSno(){ retu ...
- HMCharacteristicType 承接homekit 外包开发 微信 ELink9988
承接homekit 开发 微信 ELink9988 让HMCharacteristicTypePowerState:String配件的电源状态.该值是一个布尔值.让HMCharacteristicTy ...
- 用户控件(ASCX)向网页(ASPX)传值使用反射实现
用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现.在站点中,建一个网页以及一个用户控件. 网页切换至设计模式,拉用户控件至网页上. Default.aspx: <%@ Page La ...
- hdu 3899(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3899 思路:num[u]表示u以及u的子树的队伍数的总和,dist[u]表示u到根节点的距离,dp[u ...
- 项目期复习总结2:Table, DIV+CSS,标签嵌套规则
文件夹: 1.表格的意义,含义? 2.表格有哪些元素? 3.表格布局,表格布局的优缺点 4.行元素,块元素的差别? 5.标签的合理嵌套及标签的语义性 ① 表格的意义,含义? 表格应该用来展现那些适合表 ...