java_int数组转byte数组的一种方法
Java: int数组转byte数组的一种方法
记录一种int数组转byte数组的方法,无意中看到的。
之前都是通过移位操作完成的,现在发现通过系统API就能实现:
package com.yongdami.test;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import java.util.Locale;
public class Test {
static int[] INT_ARRY = new int[] { 0x1234, 0x5678, 0x9ABC, 0xDEF1 };
/**
*
* @param args
*/
public static void main(String[] args) {
// int 数组转 byte 数组(小端)
byte[] result1 = intArrToByteArrLittleEndian();
System.out.println("result1: " + printHexString(result1));
// int 数组转 byte 数组(小端,采用JDK API)
byte[] result2 = intArrToByteArrLittleEndianBySystemApi();
System.out.println("result2: " + printHexString(result2));
// int 数组转 byte 数组(大端)
byte[] result3 = intArrToByteArrBigEndian();
System.out.println("result3: " + printHexString(result3));
// int 数组转 byte 数组(大端,采用JDK API)
byte[] result4 = intArrToByteArrBigEndianBySystemApi();
System.out.println("result4: " + printHexString(result4));
}
static byte[] intArrToByteArrLittleEndian() {
byte[] byteArr = new byte[INT_ARRY.length * 4];
int offset = 0;
for (int i = 0; i < INT_ARRY.length; i++) {
int value = INT_ARRY[i];
byteArr[offset++] = (byte) (value & 0xFF);
byteArr[offset++] = (byte) (value >> 8 & 0xFF);
byteArr[offset++] = (byte) (value >> 16 & 0xFF);
byteArr[offset++] = (byte) (value >> 24 & 0xFF);
}
return byteArr;
}
static byte[] intArrToByteArrLittleEndianBySystemApi() {
ByteBuffer byteBuffer = ByteBuffer.allocate(INT_ARRY.length * 4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(INT_ARRY);
return byteBuffer.array();
}
static byte[] intArrToByteArrBigEndian() {
byte[] byteArr = new byte[INT_ARRY.length * 4];
int offset = 0;
for (int i = 0; i < INT_ARRY.length; i++) {
int value = INT_ARRY[i];
byteArr[offset++] = (byte) (value >> 24 & 0xFF);
byteArr[offset++] = (byte) (value >> 16 & 0xFF);
byteArr[offset++] = (byte) (value >> 8 & 0xFF);
byteArr[offset++] = (byte) (value & 0xFF);
}
return byteArr;
}
static byte[] intArrToByteArrBigEndianBySystemApi() {
ByteBuffer byteBuffer = ByteBuffer.allocate(INT_ARRY.length * 4);
byteBuffer.order(ByteOrder.BIG_ENDIAN);
IntBuffer intBuffer = byteBuffer.asIntBuffer();
intBuffer.put(INT_ARRY);
return byteBuffer.array();
}
static String printHexString(byte[] arr) {
StringBuilder builder = new StringBuilder();
for (byte b : arr) {
String s = String.format(Locale.getDefault(), "%02X ", b & 0xFF);
builder.append(s);
}
return builder.toString();
}
}
输出:
result1: 34 12 00 00 78 56 00 00 BC 9A 00 00 F1 DE 00 00
result2: 34 12 00 00 78 56 00 00 BC 9A 00 00 F1 DE 00 00
result3: 00 00 12 34 00 00 56 78 00 00 9A BC 00 00 DE F1
result4: 00 00 12 34 00 00 56 78 00 00 9A BC 00 00 DE F1
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
2020-03-16 FFmpeg: avcodec_send_packet() -1094995529, error string: Invalid data found when processing input