转换思路

1、存储大小:int是32位,byte是8位,所以要用4个byte转储

示例:

byte[0] 记录 8—1位;

byte[1] 记录 16—9位;

byte[2] 记录 24—17位;

byte[3] 记录 32—25位;

2、int直接强转为byte时,存储的是低8位。所以要完整记录32—1位,int类型的32位—9位之间需要进行移位,将其移至8—1位即可。

示例:

byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;

byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8);

byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16);

byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24);

3、如果要将负数int转换为byte,必须要考虑:移位后的符号位问题。进行移位后,在int强转为byte之前如果int类型变量为负数,就需要将高位(符号位)进行&0xff处理。

示例:

byte[0] 记录 8—1位 ——不移动——> b[0] = (byte) int;

byte[1] 记录 16—9位 ——右移8位——> b[1] = (byte) (int >> 8 & 0xff);

byte[2] 记录 24—17位 ——右移16位——> b[2] = (byte) (int >> 16 & 0xff);

byte[3] 记录 32—25位 ——右移24位——>b[3] = (byte) (int >> 24 & 0xff);

代码演示

1、int转换为byte[],不考虑正负

    public static byte[] intToByteArray(int i){byte[] bytes = new byte[4];bytes[0] = (byte) i;bytes[1] = (byte)(i >> 8);bytes[2] = (byte)(i >> 16);bytes[3] = (byte)(i >> 24);return bytes;}public static int byteArrayToInt(byte[] bytes){int res = 0;for (int i = 0; i < bytes.length; i++) {res += (bytes[i] & 0xff) << i*8;}return res;}

2、int转换为byte[],不考虑正负,采用逆序转储

    public static byte[] intToByteArrayReverse(int i){byte[] bytes = new byte[4];b[0] = (byte)(i >> 24);b[1] = (byte)(i >> 16);b[2] = (byte)(i >> 8);b[3]= (byte) i;return bytes;}public static int byteArrayToIntReverse(byte[] bytes){int value=0;for(int i = 0; i < 4; i++) {value += (bytes[i] & 0xff) << (3-i) * 8;}return value;}

3、int转换为byte[],考虑到正负,加上 &0xff

    public static byte[] intToByteArrayIgnoreSign(int i){byte[] bytes = new byte[4];bytes[0] = (byte)(i & 0xff);bytes[1] = (byte)(i >> 8 & 0xff);bytes[2] = (byte)(i >> 16 & 0xff);bytes[3] = (byte)(i >> 24 & 0xff);return bytes;}//采用循环累加也行public static int byteArrayToIntIgnoreSign(byte[] bytes){return (bytes[0] & 0xff)+((bytes[1] & 0xff) << 8)+((bytes[2] & 0xff) << 16)+((bytes[3] & 0xff) << 24);}

4、对intToByteArray()方法进行优化,用 >>> 代替 (>>、&0xff)

    public static byte[] intToByteArrayBest(int i){return new byte[]{(byte)i,(byte)(i >>> 8),(byte)(i >>> 16),(byte)(i >>> 24)};}//循环累加、或运算都可以public static int byteArrayToIntBest(byte[] bytes){return (bytes[0] & 0xff)|((bytes[1] & 0xff) << 8)|((bytes[2] & 0xff) << 16)|((bytes[3] & 0xff) << 24);}

测试过程中发现的问题

1、在byte[]转换int类型时,代码没有写对,发现意外收获

    //错误写法:少写了 &0xff//发现在(0,127)之间结果正确public static int byteArrayToInt(byte[] bytes){int value=0;for(int i = 0; i < 4; i++) {value += bytes[i] << (3-i) * 8;}return value;}//错误写法:(bytes[i] & 0xff) 少了()//发现在(0,255)之间结果正确public static int byteArrayToInt(byte[] bytes){int value=0;for(int i = 0; i < 4; i++) {value += bytes[i] & 0xff << (3-i) * 8;}return value;}

java类型转换 byte转int相关推荐

  1. Java 类型转换: char转int

    Java类型转换: char转int 源码 package com.onlydemo.javalang;/*** 类型转换: char转int* * 1.char-'0' 将char转int* 2.C ...

  2. Java | Kotlin byte转Int

    byte转Int java中是value & 0xff kotlin中要先使用.toInt()然后再and 0xff(注意:不可直接as Int) 如下: // java int i = va ...

  3. java中byte转int时候为什么要0xff

    首先先了解 byte 是8位 取值范围是-128~127 int    是32位 补码.反码.原码 补码 :原码的反码+1 反码 :原码除符号位以外的全部取反 既0变1 1变0 以上是负数的补码,正数 ...

  4. [转]java中byte转换int时为何与0xff进行与运算

    在剖析该问题前请看如下代码 public static String bytes2HexString(byte[] b) { String ret = ""; for (int i ...

  5. Java中byte转int的方法

    byte转化为int有两种情况: 1)要保持数值不变 应用场景:数值计算,等等. 方法:可以直接采用强制类型转换:int i = (int) aByte, 例如:若aByte=0xff(即数值为-1) ...

  6. Java中byte转int

    在java中,int是4个字节,但是有时我们拿到的byte是小于4个字节的,这个如何转换成int呢?下面我改进了别人的一个方法,有什么不对和可以改进的地方欢迎大家留言交流. public int by ...

  7. Java 负数byte转int

    public static void main(String[] args) throws IOException {         byte b = -127;         int c = b ...

  8. java中byte、 int、char、long、float、double各占多少字节数?

    https://blog.csdn.net/github_34402358/article/details/100120944

  9. Java中byte与16进制字符串的互相转换

    https://www.cnblogs.com/qinwangchen/p/5418028.html * Convert byte[] to hex string.这里我们可以将byte转换成int, ...

最新文章

  1. iOS将数字转成货币格式字符串
  2. java的编译及运行
  3. Java LinkedList类基本用法
  4. 《算法竞赛进阶指南》打卡-基本算法-AcWing 90. 64位整数乘法:位运算
  5. 逾期之后还能贷款吗?
  6. Nginx反向代理、动静分离、负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分...
  7. 019-Spring Boot 日志
  8. github代码管理总结
  9. spring 默认情况下事务是惟一的 同一个方法里面第一个sql开启后 在执行完 将事务传递给下一个sql...
  10. 问题五十一:怎么用ray tracing画tear drop
  11. Azure Active Directory密码同步问题
  12. 你还敢用鼠标吗?黑客在百米之外控制你的鼠标
  13. 利用灵雀云免费主机做反向ssh端口转发,实现内网服务器的外网访问_20160107_七侠镇莫小贝
  14. 趋势与新高的实战研究
  15. excel 设置表头表尾
  16. 时空旅行[线段树分治][维护凸壳]
  17. 计算机公式SUBSTITUTE,全了,SUBSTITUTE函数常用套路集合!
  18. 数据库面试题(开发者必看)
  19. Mnesia consumption
  20. 转载:Unofficial Windows Binaries for Python Extension Packages

热门文章

  1. VS Code折腾记 - (1)扯淡
  2. CPU温度多少算是正常
  3. C语言-NULL的值到底是什么?
  4. Xshell远程连接linux详细步骤(小白讲解)
  5. Python 科技研究之 04 使用 Python 简化二维三体问题模拟
  6. PL/SQL Developer 绿色版下载和安装配置
  7. 如何找出U盘中隐藏的文件夹
  8. 计算机在心理学实验中的应用举例,实验心理学试题及答案
  9. 2. (弱智问题)
  10. 2012年半程盘点之最佳iPad应用