一个int占用4个byte,所以需要声明一个4位长度的byte数组

public byte[] int2Byte2(int i) {byte[] bytes = new byte[4];bytes[0] = (byte) ((i >> 24) & 0xff);bytes[1] = (byte) ((i >> 16) & 0xff);bytes[2] = (byte) ((i >> 8) & 0xff);bytes[3] = (byte) (i & 0xff);return bytes;}
i的值 1 类型转换byte后
原码 0000 0000 0000 0000 0000 0000 0000 0001
>>24 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
>>16 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
>>8 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

0xff:0x表示16进制,后边的ff表示15*15=255,也就是1111 1111,当进行&操作时,会先将自己补位(变为int类型),也就是 0000 0000 0000 0000 0000 0000 1111 1111

&操作规则:https://blog.csdn.net/m0_47759414/article/details/107148516

这里是先进行移位再进行&操作,最后进行强转byte,此处我认为进行&操作是没必要的,因为&操作的主要目的就是将低八位以外的位置进行置0操作,但是进行强制类型转换byte后,只需要保留低八位,那么其余位置的数据是有没有置0,又有什么关系呢。

但是看到了一块apache的源码进行转换时,也进行了&操作,所以到底什么场景会用到&操作呢?

    /*** Returns the integer represented by up to 4 bytes in network byte order.* * @param buf the buffer to read the bytes from* @param start* @param count* @return*/public static int networkByteOrderToInt(byte[] buf, int start, int count) {if (count > 4) {throw new IllegalArgumentException("Cannot handle more than 4 bytes");}int result = 0;for (int i = 0; i < count; i++) {result <<= 8;result |= (buf[start + i] & 0xff);}return result;}/*** Encodes an integer into up to 4 bytes in network byte order.* * @param num the int to convert to a byte array* @param count the number of reserved bytes for the write operation* @return the resulting byte array*/public static byte[] intToNetworkByteOrder(int num, int count) {byte[] buf = new byte[count];intToNetworkByteOrder(num, buf, 0, count);return buf;}/*** Encodes an integer into up to 4 bytes in network byte order in the * supplied buffer starting at <code>start</code> offset and writing* <code>count</code> bytes.* * @param num the int to convert to a byte array* @param buf the buffer to write the bytes to* @param start the offset from beginning for the write operation* @param count the number of reserved bytes for the write operation*/public static void intToNetworkByteOrder(int num, byte[] buf, int start, int count) {if (count > 4) {throw new IllegalArgumentException("Cannot handle more than 4 bytes");}for (int i = count - 1; i >= 0; i--) {buf[start + i] = (byte) (num & 0xff);num >>>= 8;}}

怎样将int转换为byte相关推荐

  1. java int byte数组_Java 中int与byte数组转换详解

    1.与运算符的理解(&): 参加运算的两个数据,按二进位进行"与"运算.如果两个相应的二进位都为1,则该位的结果值为1,否则为0.即 0&0=0:0&1=0 ...

  2. Java中:byte转换为int ,int转换为long

    1.byte转换为int 在Java语言中,byte 和 int都是有符号的,它们的数值都是用补码做计算的. byte的数值范围是:-128 ~ +127 .高位为1表示负数,0表示正数. 如果在业务 ...

  3. byte转换为string乱码_每日一课 | 如何将int转换为String

    在Python中,我们可以使用str()将int转换为String. num1 = 100print(type(num1)) # 'int'> num2 = str(num1) print(ty ...

  4. java编程int和byte的用法_Java中的Byte Array和Int转换

    Java中的Byte Array和Int转换 我有这两个函数有一些困难: byteArrayToInt和intToByteArray . 问题是,如果我使用另一个来得到另一个结果,结果是不同的,你可以 ...

  5. java long类型值不能为0_关于原始类型:Java:为什么不能将int转换为Long

    Java中的所有数字都应为int类型. 以下行在Java> 1.5中是合法的 Short s = 1; // Will compile to Short s = Short.valueOf((s ...

  6. Java语言Int与byte[]互转详解分析

    我先贴出最终转换的代码,再来进行一步一步的介绍: /*** 将int数值转换为占四个字节的byte数组** @param value 要转换的int值* @return byte数组*/ public ...

  7. Java初认识--基本数据类型(int 和byte之间赋值)默认值 类型强转

    Java简单介绍 Java面向对象:一个面向对象的木匠关心的制作的椅子:这个木匠干了什么事情 非面向对象的木匠关心的是所使用的工具. 狗吃粮 面向对象 吃狗粮 非面向对象 Java与c++区别: 1. ...

  8. *java* 在Java中给Int类型的最大值+1 ,以及int与byte之前强转的例子

    1.给int最大值+1 public class Test1{public static void main(String[] args){//保存当前int的最大值,同理也有long maxValu ...

  9. int 和 byte 类型转换

    在java中,byte类型的取值范围是-128~127. 在c/c++中,unsigned char类型的取值范围是0~255. 当使用java代码通过jni获取到c/c++的图像数据时,图像数据一般 ...

  10. JAVA语言Integer转换为byte

    今天需要使用同事定义的函数接口碰到一个问题,eclipse提示Integer无法转换为byte. 后来想到int可以强制转换为byte.于是便思考把Integer先转换为int,然后再转换为byte. ...

最新文章

  1. conda 指定版本python_conda Pyhon版本切换
  2. deepin v20.4设置全局搜索的快捷键
  3. 将虚拟机的版本改为1.6的方法
  4. nodejs安装express框架
  5. json阅读器_Flutter小说阅读器系列一:使用Bloc模式获取起点小说关键字提示
  6. 记一次找因Redis使用不当导致应用卡死bug的过程
  7. 和与余数的和同余理解_余数与同余解析
  8. puTTY、xshell链接Linux
  9. 数字系统设计的基础知识
  10. Spine 动画工具
  11. 细心微服务架构的优势与不足那点事
  12. ServletResponse的常用方法:getWriter,setContentType
  13. 配置F5 负载均衡(转)
  14. android 模拟器测试之旅
  15. Kaggle泰坦尼克号提升准确率探索
  16. 大道至简之八:透过现象看本质(房价推手)
  17. node.js使用手册_权威的Node.js手册
  18. 计算机毕业设计java+ssm水果商城管理系统(源码+系统+mysql数据库+Lw文档)
  19. Ubuntu之Sailfish OS开发环境搭建
  20. app表白 生日 小游戏 表白

热门文章

  1. java上传文件服务器_java 实现文件上传到另一台服务器
  2. 游戏服务器开发都要学什么
  3. 希捷7200.11固件门完全DIY修正方法! 不用几块钱, DIYers请进!!!
  4. php 微信 爬虫 源码,PHP实现微信开放平台扫码登录源码下载
  5. IBM和DoE推出世界上最快的超级计算机
  6. 绿色版本chrome设为默认浏览器
  7. Mysql5.7与8.0版本不兼容问题
  8. Ubuntu18.04安装NVIDIA显卡驱动
  9. 自写string.h头文件(部分)
  10. ElasticJob