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

第一种:

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. RP2837 IN1-IN2 对应关系 2路DI

    RP2837 IN1-IN2 对应关系: IN1   ARM-IO2   PA16 IN2   ARM-IO6   PA4 root@sama5d3-linux:~ echo 16  > /sy ...

  2. Office Web App2013 在线查看PDF文件

    经常会有客户问,在SharePoint中,如何在浏览器中查看与编辑文档,通常给出的解决方案是集成Office Web App. 而在实际应用过程中,客户通常会要求实现PDF文件在线查看,对于PDF文件 ...

  3. Linux系统查看公网IP地址

    curl members.3322.org/dyndns/getip

  4. Linux快速定位并且杀掉占用端口的进程

    1.定位 lsof -i:8811(端口号) 2.杀掉进程 kill -9 63924

  5. python ascii codec can't decode

    提示错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 240: ordinal not in range ...

  6. 关于Cocos2d-x中自定义的调用注意事项

    1.在实例类Student.h中定义一个自己的方法 public: int getSno(); 2.在实例类Student.cpp中实现这个方法 int Student::getSno(){ retu ...

  7. HMCharacteristicType 承接homekit 外包开发 微信 ELink9988

    承接homekit 开发 微信 ELink9988 让HMCharacteristicTypePowerState:String配件的电源状态.该值是一个布尔值.让HMCharacteristicTy ...

  8. 用户控件(ASCX)向网页(ASPX)传值使用反射实现

    用户控件向网页传递值,方法非常之多,此博文尝试使用反射来实现.在站点中,建一个网页以及一个用户控件. 网页切换至设计模式,拉用户控件至网页上. Default.aspx: <%@ Page La ...

  9. hdu 3899(树形dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3899 思路:num[u]表示u以及u的子树的队伍数的总和,dist[u]表示u到根节点的距离,dp[u ...

  10. 项目期复习总结2:Table, DIV+CSS,标签嵌套规则

    文件夹: 1.表格的意义,含义? 2.表格有哪些元素? 3.表格布局,表格布局的优缺点 4.行元素,块元素的差别? 5.标签的合理嵌套及标签的语义性 ① 表格的意义,含义? 表格应该用来展现那些适合表 ...