这里简单记录下两种转换方式:

第一种:

1、int与byte[]之间的转换(类似的byte short,long型)

  1. /**
  2. * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。 和bytesToInt()配套使用
  3. * @param value
  4. *            要转换的int值
  5. * @return byte数组
  6. */
  7. public static byte[] intToBytes( int value )
  8. {
  9. byte[] src = new byte[4];
  10. src[3] =  (byte) ((value>>24) & 0xFF);
  11. src[2] =  (byte) ((value>>16) & 0xFF);
  12. src[1] =  (byte) ((value>>8) & 0xFF);
  13. src[0] =  (byte) (value & 0xFF);
  14. return src;
  15. }
  16. /**
  17. * 将int数值转换为占四个字节的byte数组,本方法适用于(高位在前,低位在后)的顺序。  和bytesToInt2()配套使用
  18. */
  19. public static byte[] intToBytes2(int value)
  20. {
  21. byte[] src = new byte[4];
  22. src[0] = (byte) ((value>>24) & 0xFF);
  23. src[1] = (byte) ((value>>16)& 0xFF);
  24. src[2] = (byte) ((value>>8)&0xFF);
  25. src[3] = (byte) (value & 0xFF);
  26. return src;
  27. }

byte[]转int

  1. /**
  2. * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
  3. *
  4. * @param src
  5. *            byte数组
  6. * @param offset
  7. *            从数组的第offset位开始
  8. * @return int数值
  9. */
  10. public static int bytesToInt(byte[] src, int offset) {
  11. int value;
  12. value = (int) ((src[offset] & 0xFF)
  13. | ((src[offset+1] & 0xFF)<<8)
  14. | ((src[offset+2] & 0xFF)<<16)
  15. | ((src[offset+3] & 0xFF)<<24));
  16. return value;
  17. }
  18. /**
  19. * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
  20. */
  21. public static int bytesToInt2(byte[] src, int offset) {
  22. int value;
  23. value = (int) ( ((src[offset] & 0xFF)<<24)
  24. |((src[offset+1] & 0xFF)<<16)
  25. |((src[offset+2] & 0xFF)<<8)
  26. |(src[offset+3] & 0xFF));
  27. return value;
  28. }

第二种:

1、int与byte[]之间的转换(类似的byte short,long型)

  1. /**
  2. * 将int数值转换为占四个字节的byte数组,本方法适用于(低位在前,高位在后)的顺序。
  3. * @param value
  4. *            要转换的int值
  5. * @return byte数组
  6. */
  7. public static byte[] intToBytes(int value)
  8. {
  9. byte[] byte_src = new byte[4];
  10. byte_src[3] = (byte) ((value & 0xFF000000)>>24);
  11. byte_src[2] = (byte) ((value & 0x00FF0000)>>16);
  12. byte_src[1] = (byte) ((value & 0x0000FF00)>>8);
  13. byte_src[0] = (byte) ((value & 0x000000FF));
  14. return byte_src;
  15. }

byte[]转int

    1. /**
    2. * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序。
    3. *
    4. * @param ary
    5. *            byte数组
    6. * @param offset
    7. *            从数组的第offset位开始
    8. * @return int数值
    9. */
    10. public static int bytesToInt(byte[] ary, int offset) {
    11. int value;
    12. value = (int) ((ary[offset]&0xFF)
    13. | ((ary[offset+1]<<8) & 0xFF00)
    14. | ((ary[offset+2]<<16)& 0xFF0000)
    15. | ((ary[offset+3]<<24) & 0xFF000000));
    16. return value;
    17. }

byte[]数组和int之间的转换的更多相关文章

  1. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

  2. byte数组和int之间相互转化的方法

    Java中byte数组和int类型的转换,在网络编程中这个算法是最基本的算法,我们都知道,在socket传输中,发送者接收的数据都是byte数组,但是int类型是4个byte组成的,如何把一个整形in ...

  3. 深入 JAVA里面关于byte数组和String之间的转换问题

    把byte转化成string,必须经过编码.  例如下面一个例子:  importjava.io.UnsupportedEncodingException; publicclass test{ pub ...

  4. Byte数组和Int的互相转换

    public static int bytesToInt(byte[] bytes) { int addr = bytes[0] & 0xFF; addr |= ((bytes[1] < ...

  5. java中byte数组与int类型的转换(两种方式)

    http://blog.csdn.net/z69183787/article/details/38564219 http://blog.csdn.net/z69183787/article/detai ...

  6. Java 中 byte、byte 数组和 int、long 之间的转换

    Java 中 byte 和 int 之间的转换源码: //byte 与 int 的相互转换 public static byte intToByte(int x) { return (byte) x; ...

  7. java中byte,byte[]和int之间的转换

    1>byte类型转换为,直接隐式转换,适用于要求保持数值不变,例如要求进行数值计算 如 byte b=0x01; int i=b; 2>另一种是要求保持最低字节中各个位不变,3个高字节全部 ...

  8. byte[] 数组和字符串的转换,与byte[] 数组和int类型的之间的转化

    我们先来看看byte bool  int ushort  等的定义 首先时byte[]数组与string之间的转换 string 转换位byte[] 数组 string str = "1-1 ...

  9. 如何实现数组和 List 之间的转换?(未完成)

    如何实现数组和 List 之间的转换?(未完成)

随机推荐

  1. MDI多文档窗体续

    private void 加载窗体_Click(object sender, EventArgs e) { Form2 frm = new Form2();//实例化 Form2 frm.MdiPar ...

  2. Goroutine并发调度模型深度解析之手撸一个协程池

    golanggoroutine协程池Groutine Pool高并发 并发(并行),一直以来都是一个编程语言里的核心主题之一,也是被开发者关注最多的话题:Go语言作为一个出道以来就自带 『高并发』光环 ...

  3. KMP + 求最小循环节 --- HUST 1010 - The Minimum Length

    The Minimum Length Problem's Link: http://acm.hust.edu.cn/problem/show/1010 Mean: 给你一个字符串,求这个字符串的最小循 ...

  4. Mybatis实现了接口绑定,使用更加方便。

    1.Mybatis实现了接口绑定,使用更加方便. 在ibatis2.x中我们需要在DAO的实现类中指定具体对应哪个xml映射文件, 而Mybatis实现了DAO接口与xml映射文件的绑定,自动为我们生 ...

  5. mysql根据查询结果,创建表

    create table copy_materials_details (SELECT * FROM `materials_details`);

  6. RabbitMQ之Queues-5

    工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进行在后台运行并不断的从队列中取出任务然后执行.当你运行了多个工作进 ...

  7. python3----练习......

    # 上行遍历 soup = BeautifulSoup(demo, 'html.parser') for parent in soup.a.parents: if parent is None: pr ...

  8. 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 ...

  9. ThreadLocal并不是一个Thread

    ThreadLocal是什么? 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以很简洁 ...

  10. TCP连接的建立与终止过程详解

    TCP连接的建立与终止: 1.TCP连接的建立      设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请求,然后服务进程就处于听的状态.不断检测是否有客 ...