在Java中int和byte[]的相互转换
作者:禽兽先生不禽兽 时间:2023-09-23 15:35:45
之前的项目中,在Socket通信的时候需要传int类型的值,不过java中outputsteam貌似不能直接传int类型,只能传byte[],所以在这里记录一下int和byte[]互转的方法。
/**
* int转byte[]
*/
public static byte[] intToBytes(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;
}
接收的时候再转一下即可
/**
* byte[]转int
*/
public static int bytesToInt(byte[] bytes) {
int i;
i = (int) ((bytes[0] & 0xff) | ((bytes[1] & 0xff) << 8)
| ((bytes[2] & 0xff) << 16) | ((bytes[3] & 0xff) << 24));
return i;
}
以上所述是小编给大家介绍的在Java中int和byte[]的相互转换网站的支持!
来源:http://blog.csdn.net/zgcqflqinhao/article/details/53256862
标签:byte,int,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Javaweb 定时器功能代码实例
2022-04-07 00:47:20
SpringBoot中异常处理实战记录
2022-07-21 20:39:01
![](https://img.aspxhome.com/file/2023/2/121932_0s.png)
Spring集成Quartz的简单配置的方法
2023-08-24 02:52:27
Java/Web调用Hadoop进行MapReduce示例代码
2023-09-01 05:05:50
![](https://img.aspxhome.com/file/2023/3/77093_0s.png)
Java中List与数组之间的相互转换
2023-09-10 03:44:28
【IntelliJ IDEA】Maven构建自己的第一个Java后台的方法
2023-09-24 20:11:16
![](https://img.aspxhome.com/file/2023/5/83015_0s.png)
springmvc 参数绑定总结
2023-11-16 21:30:44
Java实现两人五子棋游戏(二) 画出棋盘
2022-08-26 15:09:57
![](https://img.aspxhome.com/file/2023/3/63383_0s.png)
springboot调用支付宝第三方接口(沙箱环境)
2023-11-25 06:12:08
![](https://img.aspxhome.com/file/2023/8/58748_0s.png)
Android listview的滑动冲突解决方法
2022-07-19 02:50:59
java二维数组基础知识详解
2023-03-28 11:10:54
![](https://img.aspxhome.com/file/2023/9/63999_0s.png)
Unity UI实现拖拽旋转
2023-04-10 06:44:59
![](https://img.aspxhome.com/file/2023/9/74839_0s.jpg)
spring boot使用拦截器修改请求URL域名 换 IP 访问的方法
2022-08-21 20:15:53
java内存泄漏与内存溢出关系解析
2023-08-07 01:58:59
详解Java中Iterable与Iterator用法
2022-05-14 04:25:30
Spring框架基于注解开发CRUD详解
2021-08-04 03:39:03
![](https://img.aspxhome.com/file/2023/7/111447_0s.jpg)
Android天气预报app改进版
2023-12-04 05:54:07
![](https://img.aspxhome.com/file/2023/1/139021_0s.gif)
WPF InkCanvas基本操作方法详解
2023-07-29 06:26:59
![](https://img.aspxhome.com/file/2023/1/70441_0s.gif)
Android自定义View实现投票进度条
2022-04-02 13:57:21
![](https://img.aspxhome.com/file/2023/9/139489_0s.jpg)
Springboot深入讲解nocos的整合与使用
2022-06-05 05:11:27
![](https://img.aspxhome.com/file/2023/2/114042_0s.png)