java byte数组与int,long,short,byte的转换实现方法
作者:jingxian 发布时间:2023-09-08 12:26:43
标签:byte,short,int,long
实例如下:
public class DataTypeChangeHelper {
/**
* 将一个单字节的byte转换成32位的int
*
* @param b
* byte
* @return convert result
*/
public static int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
/**
* 将一个单字节的Byte转换成十六进制的数
*
* @param b
* byte
* @return convert result
*/
public static String byteToHex(byte b) {
int i = b & 0xFF;
return Integer.toHexString(i);
}
/**
* 将一个4byte的数组转换成32位的int
*
* @param buf
* bytes buffer
* @param byte[]中开始转换的位置
* @return convert result
*/
public static long unsigned4BytesToInt(byte[] buf, int pos) {
int firstByte = 0;
int secondByte = 0;
int thirdByte = 0;
int fourthByte = 0;
int index = pos;
firstByte = (0x000000FF & ((int) buf[index]));
secondByte = (0x000000FF & ((int) buf[index + 1]));
thirdByte = (0x000000FF & ((int) buf[index + 2]));
fourthByte = (0x000000FF & ((int) buf[index + 3]));
index = index + 4;
return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;
}
/**
* 将16位的short转换成byte数组
*
* @param s
* short
* @return byte[] 长度为2
* */
public static byte[] shortToByteArray(short s) {
byte[] targets = new byte[2];
for (int i = 0; i < 2; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* 将32位整数转换成长度为4的byte数组
*
* @param s
* int
* @return byte[]
* */
public static byte[] intToByteArray(int s) {
byte[] targets = new byte[2];
for (int i = 0; i < 4; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**
* long to byte[]
*
* @param s
* long
* @return byte[]
* */
public static byte[] longToByteArray(long s) {
byte[] targets = new byte[2];
for (int i = 0; i < 8; i++) {
int offset = (targets.length - 1 - i) * 8;
targets[i] = (byte) ((s >>> offset) & 0xff);
}
return targets;
}
/**32位int转byte[]*/
public static byte[] int2byte(int res) {
byte[] targets = new byte[4];
targets[0] = (byte) (res & 0xff);// 最低位
targets[1] = (byte) ((res >> 8) & 0xff);// 次低位
targets[2] = (byte) ((res >> 16) & 0xff);// 次高位
targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。
return targets;
}
/**
* 将长度为2的byte数组转换为16位int
*
* @param res
* byte[]
* @return int
* */
public static int byte2int(byte[] res) {
// res = InversionByte(res);
// 一个byte数据左移24位变成0x??000000,再右移8位变成0x00??0000
int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或
return targets;
}
}


猜你喜欢
- SpringMVC异常处理机制(一)项目前准备首先参照文章Spring课程工程构建+SpringMVC简介及其快速入门搭建项目搭建好一个项目
- Spring Boot 解决富文本上传图片跨域在前后端分离的情况下,后台所写的接口在前端调用的时候,可能前端浏览器已经读取到了数据,但是在前
- 前言在开发过程中,会遇到很多的实体需要将查出的数据处理为下拉或者级联下拉的结构,提供给前端进行展示。在数据库查出的结构中,可能是集合<
- 时间轮关于时间轮的介绍,网上有很多,这里就不重复了核心思想一个环形数组存储时间轮的所有槽(看你的手表),每个槽对应当前时间轮的最小精度超过当
- Springcloud Config什么是springcloud Config  简单来说,Spring
- 本文实例为大家分享了Android学习之Broadcast的使用方法,供大家参考,具体内容如下实现开机启动提示网络的广播package co
- 前言:本文介绍Java中数组转为List三种情况的优劣对比,以及应用场景的对比,以及程序员常犯的类型转换错误原因解析。一.最常见方式(未必最
- 在用C#开发Web应用时有个痛点,就是本机用VS开启Web应用调试时外部机器无法访问此Web应用。这里将会介绍如何通过设置允许局域网和外网机
- 1、Pull概述Android系统中和创建XML相关的包为org.xmlpull.v1,在这个包中不仅提供了用于创建XML的 X
- 作为一个ORM框架,hibernate肯定也需要满足我们实现表与表之间进行关联的需要。hibernate在关联方法的实现很简单。下面我们先来
- 前言众所周知,黄油刀 ButterKnife 已经废弃了,并且已经不再维护了,而一些老项目估计还有一堆这样的代码,相信大家多多少少都有过被
- (注意:本文基于JDK1.8)前言任何一个容器类对象用于持有元素后,总是需要遍历元素的,即挨个去访问每个元素1次,而遍历元素,除了常规的依赖
- 主要介绍使用MediaPlayer播放音频的方式。关于MediaPlayer的基础知识,比如状态,可以参考Android MediaPlay
- 迭代器模式,一直没用过,也不会用。恰巧MyBatis框架中也使用到了迭代器模式,而且看起来还比较简单,在以后的工作中,若有需要咱们可模仿它的
- 下面通过代码看下JAVA查询树结构数据(省市区)使用hutool工具实现代码:@PostMapping("/getTree&quo
- 什么是优雅停机先来一段简单的代码,如下:@RestControllerpublic class DemoController { @GetM
- 简介在 io 包中,提供了两个与平台无关的数据操作流:数据输出流(DataOutputStream)、数据输入流 (DataInputStr
- 前言使用基于TCP 协议的双向通信时,网络中的两个应用程序之间必须首先建立一个连接,这两个程序通过一个双向的通信连接实现数据的交换,这个连接
- 用Linq从一个集合选取几列得到一个新的集合-可改列名
- 本文实例讲述了C#实现多线程下载文件的方法。分享给大家供大家参考。具体实现方法如下:using System;using System.Co