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 之间的转换?(未完成)
随机推荐
- koa介绍
https://github.com/koajs/koa https://github.com/demopark/koa-docs-Zh-CN ctx.response.type = 'json'; ...
- PHP多进程(4) :内部多进程
说的都是只兼容unix 服务器的多进程,下面来讲讲在window 和 unix 都兼容的多进程(这里是泛指,下面的curl实际上是通过IO复用实现的). 通过扩展实现多线程的典型例子是CURL,CUR ...
- 敏捷软件开发实践-Release Process/Release Plan(转)
介绍: 因为我们的开发周期是迭代进行的,以Sprint为单位,我们每个 Sprint如何去和客户说我们的成果呢,那么我就需要Demo和release一些新功能,或者一些bug fixing.Demo我 ...
- C# 创建XML文件
private void CreateXMLFile(string pathAndFileName) { XmlDocument doc = new XmlDocument(); XmlElement ...
- (转)The windows boot configuration data file dose not contain a valid OS entry
开价蓝屏,显示: Windows failed to start. A recent hardware or software change might be the case.to fix the ...
- Scilab 的画图函数(3)
我们在做数据画图或函数图像时常常须要使用对数坐标系.尤其是数据的范围跨越非常多个数量级时.通常的线性坐标系下无法表现出数据特征. Scilab 中Plot函数无法画出对数坐标.须要使用 plot2d ...
- Android Fragment Base
public class FragmentTabsActivity extends FragmentActivity implements OnClickListener { //定义Fragment ...
- JDBC通用DAO
dbcBaseDao接口,内容如下: package com.sun4j.core.jdbc.dao; import java.io.Serializable; import java.util.Li ...
- Gson简要使用笔记(转载)
经过比较,gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需 ...
- LeetCode 笔记系列一 Median of Two Sorted Arrays
题目:There are two sorted arrays A and B of size m and n respectively. Find the median of the two sort ...