【详解】基本数据类型与byte数组相互转化
基本数据类型与byte数组相互转化
在编程中,尤其是在网络通信、文件读写等场景下,经常需要将基本数据类型(如int、long、double等)转换为字节数组(byte array),或者从字节数组中恢复基本数据类型。本文将详细介绍Java中如何实现这些转换。
1. 基本数据类型转byte数组
1.1 int转byte数组
将一个int类型的值转换为byte数组,可以使用位运算来实现:
public static byte[] intToBytes(int value) {
return new byte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
1.2 long转byte数组
将一个long类型的值转换为byte数组,同样可以使用位运算:
public static byte[] longToBytes(long value) {
return new byte[]{
(byte) (value >>> 56),
(byte) (value >>> 48),
(byte) (value >>> 40),
(byte) (value >>> 32),
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
1.3 double转byte数组
将一个double类型的值转换为byte数组,可以先将其转换为long,然后再使用上述方法:
public static byte[] doubleToBytes(double value) {
long longValue = Double.doubleToLongBits(value);
return longToBytes(longValue);
}
2. byte数组转基本数据类型
2.1 byte数组转int
将一个byte数组转换为int类型的值,可以使用位运算和移位操作:
public static int bytesToInt(byte[] bytes) {
return (bytes[0] << 24) |
(bytes[1] << 16) |
(bytes[2] << 8) |
(bytes[3]);
}
2.2 byte数组转long
将一个byte数组转换为long类型的值,同样可以使用位运算和移位操作:
public static long bytesToLong(byte[] bytes) {
return ((long) bytes[0] << 56) |
((long) bytes[1] << 48) |
((long) bytes[2] << 40) |
((long) bytes[3] << 32) |
((long) bytes[4] << 24) |
((long) bytes[5] << 16) |
((long) bytes[6] << 8) |
((long) bytes[7]);
}
2.3 byte数组转double
将一个byte数组转换为double类型的值,可以先将其转换为long,然后再使用Double.longBitsToDouble方法:
public static double bytesToDouble(byte[] bytes) {
long longValue = bytesToLong(bytes);
return Double.longBitsToDouble(longValue);
}
3. 示例代码
以下是一个完整的示例代码,展示了如何使用上述方法进行基本数据类型和byte数组之间的转换:
public class DataTypeConversion {
public static void main(String[] args) {
// int to byte array
int intValue = 123456;
byte[] intBytes = intToBytes(intValue);
System.out.println("int to byte array: " + bytesToHex(intBytes));
// byte array to int
int intValueFromBytes = bytesToInt(intBytes);
System.out.println("byte array to int: " + intValueFromBytes);
// long to byte array
long longValue = 1234567890123456789L;
byte[] longBytes = longToBytes(longValue);
System.out.println("long to byte array: " + bytesToHex(longBytes));
// byte array to long
long longValueFromBytes = bytesToLong(longBytes);
System.out.println("byte array to long: " + longValueFromBytes);
// double to byte array
double doubleValue = 123.456;
byte[] doubleBytes = doubleToBytes(doubleValue);
System.out.println("double to byte array: " + bytesToHex(doubleBytes));
// byte array to double
double doubleValueFromBytes = bytesToDouble(doubleBytes);
System.out.println("byte array to double: " + doubleValueFromBytes);
}
public static byte[] intToBytes(int value) {
return new byte[]{
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
public static byte[] longToBytes(long value) {
return new byte[]{
(byte) (value >>> 56),
(byte) (value >>> 48),
(byte) (value >>> 40),
(byte) (value >>> 32),
(byte) (value >>> 24),
(byte) (value >>> 16),
(byte) (value >>> 8),
(byte) value
};
}
public static byte[] doubleToBytes(double value) {
long longValue = Double.doubleToLongBits(value);
return longToBytes(longValue);
}
public static int bytesToInt(byte[] bytes) {
return (bytes[0] << 24) |
(bytes[1] << 16) |
(bytes[2] << 8) |
(bytes[3]);
}
public static long bytesToLong(byte[] bytes) {
return ((long) bytes[0] << 56) |
((long) bytes[1] << 48) |
((long) bytes[2] << 40) |
((long) bytes[3] << 32) |
((long) bytes[4] << 24) |
((long) bytes[5] << 16) |
((long) bytes[6] << 8) |
((long) bytes[7]);
}
public static double bytesToDouble(byte[] bytes) {
long longValue = bytesToLong(bytes);
return Double.longBitsToDouble(longValue);
}
public static String bytesToHex(byte[] bytes) {
StringBuilder hexString = new StringBuilder();
for (byte b : bytes) {
String hex = Integer.toHexString(0xFF & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}
以上是关于基本数据类型与byte数组相互转化的技术博客文章。希望对你有所帮助!当然可以!在Java中,基本数据类型(如int、float、double等)和byte数组之间的转换是常见的需求,尤其是在网络通信、文件读写等场景中。下面我将分别给出几个示例,展示如何将基本数据类型转换为byte数组,以及如何从byte数组恢复基本数据类型。
1. int 和 byte[] 的转换
int 转换为 byte[]
public class IntToByteArray {
public static void main(String[] args) {
int number = 123456789;
byte[] bytes = new byte[4];
// 将int转换为byte数组
for (int i = 0; i < 4; i++) {
bytes[i] = (byte) ((number >> (i * 8)) & 0xFF);
}
System.out.println("Byte array: " + Arrays.toString(bytes));
}
}
byte[] 转换为 int
public class ByteArrayToInt {
public static void main(String[] args) {
byte[] bytes = { -86, 46, -42, 77 }; // 这个数组是从上面的例子得到的
int number = 0;
// 将byte数组转换为int
for (int i = 0; i < 4; i++) {
number |= (bytes[i] & 0xFF) << (i * 8);
}
System.out.println("Number: " + number);
}
}
2. float 和 byte[] 的转换
float 转换为 byte[]
public class FloatToByteArray {
public static void main(String[] args) {
float number = 3.14f;
byte[] bytes = new byte[4];
// 将float转换为byte数组
ByteBuffer buffer = ByteBuffer.allocate(4);
buffer.putFloat(number);
buffer.flip(); // 翻转缓冲区
buffer.get(bytes);
System.out.println("Byte array: " + Arrays.toString(bytes));
}
}
byte[] 转换为 float
public class ByteArrayToFloat {
public static void main(String[] args) {
byte[] bytes = { 64, 9, 21, 115 }; // 这个数组是从上面的例子得到的
float number = 0.0f;
// 将byte数组转换为float
ByteBuffer buffer = ByteBuffer.wrap(bytes);
number = buffer.getFloat();
System.out.println("Number: " + number);
}
}
3. double 和 byte[] 的转换
double 转换为 byte[]
public class DoubleToByteArray {
public static void main(String[] args) {
double number = 3.141592653589793;
byte[] bytes = new byte[8];
// 将double转换为byte数组
ByteBuffer buffer = ByteBuffer.allocate(8);
buffer.putDouble(number);
buffer.flip(); // 翻转缓冲区
buffer.get(bytes);
System.out.println("Byte array: " + Arrays.toString(bytes));
}
}
byte[] 转换为 double
public class ByteArrayToDouble {
public static void main(String[] args) {
byte[] bytes = { 64, 9, 21, 115, -31, -11, -10, -21 }; // 这个数组是从上面的例子得到的
double number = 0.0;
// 将byte数组转换为double
ByteBuffer buffer = ByteBuffer.wrap(bytes);
number = buffer.getDouble();
System.out.println("Number: " + number);
}
}
在Java中,基本数据类型(如int, double, char, boolean等)和byte[]之间的转换是一个常见的需求,尤其是在处理网络通信、文件读写或加密解密时。下面将详细介绍如何在Java中实现这些转换。
1. 基本数据类型转 byte[]
1.1 int 转 byte[]
public static byte[] intToByteArray(int value) {
return new byte[] {
(byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF)
};
}
1.2 long 转 byte[]
public static byte[] longToByteArray(long value) {
return new byte[] {
(byte)((value >> 56) & 0xFF),
(byte)((value >> 48) & 0xFF),
(byte)((value >> 40) & 0xFF),
(byte)((value >> 32) & 0xFF),
(byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF)
};
}
1.3 float 转 byte[]
public static byte[] floatToByteArray(float value) {
return intToByteArray(Float.floatToIntBits(value));
}
1.4 double 转 byte[]
public static byte[] doubleToByteArray(double value) {
return longToByteArray(Double.doubleToLongBits(value));
}
1.5 char 转 byte[]
public static byte[] charToByteArray(char value) {
return new byte[] {
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF)
};
}
1.6 boolean 转 byte[]
public static byte[] booleanToByteArray(boolean value) {
return new byte[] { (byte)(value ? 1 : 0) };
}
2. byte[] 转基本数据类型
2.1 byte[] 转 int
public static int byteArrayToInt(byte[] bytes) {
return (bytes[0] & 0xFF) << 24 |
(bytes[1] & 0xFF) << 16 |
(bytes[2] & 0xFF) << 8 |
(bytes[3] & 0xFF);
}
2.2 byte[] 转 long
public static long byteArrayToLong(byte[] bytes) {
return (bytes[0] & 0xFFL) << 56 |
(bytes[1] & 0xFFL) << 48 |
(bytes[2] & 0xFFL) << 40 |
(bytes[3] & 0xFFL) << 32 |
(bytes[4] & 0xFFL) << 24 |
(bytes[5] & 0xFFL) << 16 |
(bytes[6] & 0xFFL) << 8 |
(bytes[7] & 0xFFL);
}
2.3 byte[] 转 float
public static float byteArrayToFloat(byte[] bytes) {
return Float.intBitsToFloat(byteArrayToInt(bytes));
}
2.4 byte[] 转 double
public static double byteArrayToDouble(byte[] bytes) {
return Double.longBitsToDouble(byteArrayToLong(bytes));
}
2.5 byte[] 转 char
public static char byteArrayToChar(byte[] bytes) {
return (char)(((bytes[0] & 0xFF) << 8) | (bytes[1] & 0xFF));
}
2.6 byte[] 转 boolean
public static boolean byteArrayToBoolean(byte[] bytes) {
return bytes[0] != 0;
}
3. 使用 ByteBuffer 进行转换
Java的ByteBuffer类提供了更方便的方法来进行基本数据类型和byte[]之间的转换。以下是一些示例:
3.1 int 转 byte[]
import java.nio.ByteBuffer;
public static byte[] intToByteArray(int value) {
return ByteBuffer.allocate(4).putInt(value).array();
}
3.2 byte[] 转 int
public static int byteArrayToInt(byte[] bytes) {
return ByteBuffer.wrap(bytes).getInt();
}
3.3 double 转 byte[]
public static byte[] doubleToByteArray(double value) {
return ByteBuffer.allocate(8).putDouble(value).array();
}
3.4 byte[] 转 double
public static double byteArrayToDouble(byte[] bytes) {
return ByteBuffer.wrap(bytes).getDouble();
}
4. 注意事项
- 字节序:上述示例默认使用大端字节序(Big-Endian)。如果需要小端字节序(Little-Endian),可以在
ByteBuffer中调用order(ByteOrder.LITTLE_ENDIAN)方法。 - 异常处理:在实际应用中,建议添加异常处理,以应对可能的输入错误或异常情况。

通过这些方法,你可以在Java中轻松地进行基本数据类型和byte[]之间的转换。希望这些示例对你有所帮助!
- 点赞
- 收藏
- 关注作者
2.2 byte数组转long
评论(0)