Java 中 byte、byte 数组和 int、long 之间的转换
Java 中 byte 和 int 之间的转换源码:
- //byte 与 int 的相互转换
- public static byte intToByte(int x) {
- return (byte) x;
- }
- public static int byteToInt(byte b) {
- //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
- return b & 0xFF;
- }
测试代码:
- //测试 int 转 byte
- int int0 = 234;
- byte byte0 = intToByte(int0);
- System.out.println("byte0=" + byte0);//byte0=-22
- //测试 byte 转 int
- int int1 = byteToInt(byte0);
- System.out.println("int1=" + int1);//int1=234
Java 中 byte 数组和 int 之间的转换源码:
- //byte 数组与 int 的相互转换
- public static int byteArrayToInt(byte[] b) {
- return b[3] & 0xFF |
- (b[2] & 0xFF) << 8 |
- (b[1] & 0xFF) << 16 |
- (b[0] & 0xFF) << 24;
- }
- public static byte[] intToByteArray(int a) {
- return new byte[] {
- (byte) ((a >> 24) & 0xFF),
- (byte) ((a >> 16) & 0xFF),
- (byte) ((a >> 8) & 0xFF),
- (byte) (a & 0xFF)
- };
- }
测试代码:
- //测试 int 转 byte 数组
- int int2 = 1417;
- byte[] bytesInt = intToByteArray(int2);
- System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
- //测试 byte 数组转 int
- int int3 = byteArrayToInt(bytesInt);
- System.out.println("int3=" + int3);//int3=1417
Java 中 byte 数组和 long 之间的转换源码:
- private static ByteBuffer buffer = ByteBuffer.allocate(8);
- //byte 数组与 long 的相互转换
- public static byte[] longToBytes(long x) {
- buffer.putLong(0, x);
- return buffer.array();
- }
- public static long bytesToLong(byte[] bytes) {
- buffer.put(bytes, 0, bytes.length);
- buffer.flip();//need flip
- return buffer.getLong();
- }
测试代码:
- //测试 long 转 byte 数组
- long long1 = 2223;
- byte[] bytesLong = longToBytes(long1);
- System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
- //测试 byte 数组 转 long
- long long2 = bytesToLong(bytesLong);
- System.out.println("long2=" + long2);//long2=2223
整体工具类源码:
- import java.nio.ByteBuffer;
- public class Test {
- private static ByteBuffer buffer = ByteBuffer.allocate(8);
- public static void main(String[] args) {
- //测试 int 转 byte
- int int0 = 234;
- byte byte0 = intToByte(int0);
- System.out.println("byte0=" + byte0);//byte0=-22
- //测试 byte 转 int
- int int1 = byteToInt(byte0);
- System.out.println("int1=" + int1);//int1=234
- //测试 int 转 byte 数组
- int int2 = 1417;
- byte[] bytesInt = intToByteArray(int2);
- System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
- //测试 byte 数组转 int
- int int3 = byteArrayToInt(bytesInt);
- System.out.println("int3=" + int3);//int3=1417
- //测试 long 转 byte 数组
- long long1 = 2223;
- byte[] bytesLong = longToBytes(long1);
- System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
- //测试 byte 数组 转 long
- long long2 = bytesToLong(bytesLong);
- System.out.println("long2=" + long2);//long2=2223
- }
- //byte 与 int 的相互转换
- public static byte intToByte(int x) {
- return (byte) x;
- }
- public static int byteToInt(byte b) {
- //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
- return b & 0xFF;
- }
- //byte 数组与 int 的相互转换
- public static int byteArrayToInt(byte[] b) {
- return b[3] & 0xFF |
- (b[2] & 0xFF) << 8 |
- (b[1] & 0xFF) << 16 |
- (b[0] & 0xFF) << 24;
- }
- public static byte[] intToByteArray(int a) {
- return new byte[] {
- (byte) ((a >> 24) & 0xFF),
- (byte) ((a >> 16) & 0xFF),
- (byte) ((a >> 8) & 0xFF),
- (byte) (a & 0xFF)
- };
- }
- //byte 数组与 long 的相互转换
- public static byte[] longToBytes(long x) {
- buffer.putLong(0, x);
- return buffer.array();
- }
- public static long bytesToLong(byte[] bytes) {
- buffer.put(bytes, 0, bytes.length);
- buffer.flip();//need flip
- return buffer.getLong();
- }
- }
运行测试结果:
byte0=-22
int1=234
bytesInt=[B@de6ced
int3=1417
bytes=[B@c17164
long2=2223
参考文章1:http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back。
参考文章2:http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java。
参考文章3:http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java。
Java 中 byte、byte 数组和 int、long 之间的转换的更多相关文章
- C++string,char* 字符数组,int类型之间的转换
string.int 常见类型之间相互转换 int & string 之间的转换 C++中更多的是使用流对象来实现类型转换 针对流对象 sstream实现 int,float 类型都可以实现 ...
- Java中的基本数据类型和基本数据类型之间的转换
在Java中有8中基本数据类型,分别为: 整型: byte.short.int.long 浮点型:float.double 布尔型:boolean 字符型:char. byte: 8位, 封装 ...
- java中的字符,字符串,数字之间的转换
string 和int之间的转换 string转换成int :Integer.valueOf("12") int转换成string : String.valueOf(12) ch ...
- java中的字符、字符串及数字之间的转换(转)
一.string 和int之间的转换 1.string转换成int :Integer.valueOf("12") 2.int转换成string : String.valueOf( ...
- 详解java中的byte类型
Java也提供了一个byte数据类型,并且是基本类型.java byte是做为最小的数字来处理的,因此它的值域被定义为-128~127,也就是signed byte.下面这篇文章主要给大家介绍了关于j ...
- java中的byte
8 bit (位) = 1 Byte (字节) java中的byte就是Byte 1024 Byte = 1KB 1024 KB = 1MB 1:“字节”是byte,“位”是bit : 2: 1 by ...
- 深入理解java中的byte类型
作者 | 进击的石头--GO! 来源 | https://www.cnblogs.com/zl181015/p/9435035.html#4432849 Java也提供了一个byte数据类型,并且是基 ...
- 谈谈Java中整数类型(short int long)的存储方式
在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对ja ...
- java中整数的默认为int类型的一些问题
thingking in java 读书感悟 作者 :淮左白衣 写于2018年4月8日17:51:44 关于整数的默认类型,以及会产生的一些小问题 涉及基本数据类型的重载 关于整数的默认类型,以及会产 ...
- Java中创建泛型数组
Java中创建泛型数组 使用泛型时,我想很多人肯定尝试过如下的代码,去创建一个泛型数组 T[] array = new T[]; 当我们写出这样的代码时编译器会报Cannot create a gen ...
随机推荐
- 【CTF WEB】服务端请求伪造
服务端请求伪造 如你所愿,这次可以读取所有的图片,但是域名必须是www开头 测试方法 POST /index.php HTTP/1.1 Host: 218.2.197.236:27375 Conten ...
- 利用 devcon.exe实现自动安装驱动(转)
http://blog.csdn.net/u012814201/article/details/44919125 工作的原因打算通过devcon.exe实现自动打包的功能,由于之前一直在Linux那个 ...
- poj2049
优先队列广搜,有人说用SPFA,不知道怎么做的 #include <cstdio> #include <queue> #include <cmath> #inclu ...
- 11 Go 1.11 Release Notes
Go 1.11 Release Notes Introduction to Go 1.11 Changes to the language Ports WebAssembly RISC-V GOARC ...
- Android BLE设备蓝牙通信框架BluetoothKit
BluetoothKit是一款功能强大的Android蓝牙通信框架,支持低功耗蓝牙设备的连接通信.蓝牙广播扫描及Beacon解析. 关于该项目的详细文档请关注:https://github.com/d ...
- hdu 5428 质因子
问题描述有一个数列,FancyCoder沉迷于研究这个数列的乘积相关问题,但是它们的乘积往往非常大.幸运的是,FancyCoder只需要找到这个巨大乘积的最小的满足如下规则的因子:这个因子包含大于两个 ...
- K8s中,tomcat的一部分jvm参数,如何通过env环境变量传递?
这两天解决的一个需求: 如果用户没有在deployment中设置env参数,则tomcat默认使用1G左右的内存: 如果用户在deployment中提供了jvm参数,则tomcat将这部分的参数,覆盖 ...
- WebApi 接口参数详解
WebApi 接口参数不再困惑:传参详解 阅读目录 一.get请求 1.基础类型参数 2.实体作为参数 3.数组作为参数 4.“怪异”的get请求 二.post请求 1.基础类型参数 2.实体作为 ...
- 工作流调度器azkaban2.5.0的安装和使用
为什么需要工作流调度系统 一个完整的数据分析系统通常都是由大量任务单元组成: shell脚本程序,java程序,mapreduce程序.hive脚本等 各任务单元之间存在时间先后及前后依赖关系 为了很 ...
- 020 shuffle的重要作用,以及分区的实践
一:学shuffle原理的必要性 1.说明 学习shuffle的作用是可以对程序进行优化. 在shuffle这个部分有三个部分需要注意: 分区 排序 分组 这个可以进行优化. 二:分区的实践 1.说明 ...