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 之间的转换?(未完成)
随机推荐
- MDI多文档窗体续
private void 加载窗体_Click(object sender, EventArgs e) { Form2 frm = new Form2();//实例化 Form2 frm.MdiPar ...
- Goroutine并发调度模型深度解析之手撸一个协程池
golanggoroutine协程池Groutine Pool高并发 并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环 ...
- KMP + 求最小循环节 --- HUST 1010 - The Minimum Length
The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...
- Mybatis实现了接口绑定,使用更加方便。
1.Mybatis实现了接口绑定,使用更加方便. 在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件, 而Mybatis实现了DAO接口与xml映射文件的绑定,自动为我们生 ...
- mysql根据查询结果,创建表
create table copy_materials_details (SELECT * FROM `materials_details`);
- RabbitMQ之Queues-5
工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进行在后台运行并不断的从队列中取出任务然后执行.当你运行了多个工作进 ...
- python3----练习......
# 上行遍历 soup = BeautifulSoup(demo, 'html.parser') for parent in soup.a.parents: if parent is None: pr ...
- Codeforces Round #296 (Div. 2) B. Error Correct System
B. Error Correct System time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- ThreadLocal并不是一个Thread
ThreadLocal是什么? 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁 ...
- TCP连接的建立与终止过程详解
TCP连接的建立与终止: 1.TCP连接的建立 设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请求,然后服务进程就处于听的状态.不断检测是否有客 ...