杰瑞科技汇

Java toByteArray()如何正确使用?

Of course! In Java, converting data to a byte[] (a byte array) is a very common task. The method you're likely thinking of is String.getBytes(), but there are many other ways to convert different data types into byte arrays.

Java toByteArray()如何正确使用?-图1
(图片来源网络,侵删)

Let's break down the most common scenarios.


Converting a String to a byte[]

This is the most frequent use case. The key thing to remember is that characters in Java are encoded using UTF-16, but you often need to convert a string to a specific byte encoding like UTF-8.

Method 1: String.getBytes(Charset) (Recommended)

This is the most explicit and recommended way. It prevents platform-specific behavior and allows you to specify the exact character encoding.

import java.nio.charset.StandardCharsets;
public class StringToByteArray {
    public static void main(String[] args) {
        String text = "Hello, 世界!"; // String with English and Chinese characters
        // Convert to byte array using UTF-8 encoding
        byte[] utf8Bytes = text.getBytes(StandardCharsets.UTF_8);
        System.out.println("UTF-8 Bytes: " + java.util.Arrays.toString(utf8Bytes));
        // Convert to byte array using ISO-8859-1 (Latin-1) encoding
        byte[] latin1Bytes = text.getBytes(StandardCharsets.ISO_8859_1);
        System.out.println("ISO-8859-1 Bytes: " + java.util.Arrays.toString(latin1Bytes));
    }
}

Output:

Java toByteArray()如何正确使用?-图2
(图片来源网络,侵删)
UTF-8 Bytes: [72, 101, 108, 108, 111, 44, 32, -28, -72, -83, -27, -91, -67, 33]
ISO-8859-1 Bytes: [72, 101, 108, 108, 111, 44, 32, 63, 63, 63, 63, 63, 33]

Notice how the Chinese characters are represented correctly in UTF-8 but are replaced with in ISO-8859-1 because that encoding doesn't support them.

Method 2: String.getBytes() (Legacy, Avoid in new code)

This method uses the platform's default character encoding. This can lead to bugs if your code runs on different operating systems (e.g., Windows often uses Cp1252 by default, while Linux uses UTF-8).

// NOT RECOMMENDED for production code
String text = "Hello";
byte[] bytes = text.getBytes(); // Relies on the default charset

Method 3: String.getBytes(String charsetName) (Throws Exception)

This is similar to the recommended method but takes the charset name as a String. It throws an UnsupportedEncodingException if the charset is not available, so it requires a try-catch block.

String text = "Hello";
try {
    byte[] bytes = text.getBytes("UTF-8");
    System.out.println("Bytes from name: " + java.util.Arrays.toString(bytes));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

Converting a Primitive Type (e.g., int, long) to a byte[]

For numbers, you need to handle the fact that Java primitives are bigger than a single byte (an int is 4 bytes, a long is 8 bytes). The conversion involves breaking the number down into its constituent bytes.

Java toByteArray()如何正确使用?-图3
(图片来源网络,侵删)

Using ByteBuffer (Modern & Recommended)

The java.nio.ByteBuffer class is the cleanest and safest way to handle this. It handles endianness (byte order) for you.

import java.nio.ByteBuffer;
public class PrimitiveToByteArray {
    public static void main(String[] args) {
        int number = 0x12345678; // An integer value
        // Create a ByteBuffer with a capacity of 4 bytes (size of an int)
        ByteBuffer buffer = ByteBuffer.allocate(4);
        buffer.putInt(number); // Put the integer into the buffer
        // Get the underlying byte array
        byte[] byteArray = buffer.array();
        System.out.println("Original int: " + number);
        System.out.println("Byte array: " + java.util.Arrays.toString(byteArray));
    }
}

Output (on a Little-Endian machine like x86/x64):

Original int: 305419896
Byte array: [120, 86, 52, 18]

The bytes are in little-endian order (least significant byte first).

Manual Conversion (For Understanding)

Here's how you would do it manually. This is useful for learning but ByteBuffer is preferred for real code.

public class IntToByteArrayManual {
    public static void main(String[] args) {
        int value = 0x12345678;
        byte[] bytes = new byte[4];
        // Manually shift and mask to get each byte
        bytes[0] = (byte) (value >> 24); // Most significant byte
        bytes[1] = (byte) (value >> 16);
        bytes[2] = (byte) (value >> 8);
        bytes[3] = (byte) value; // Least significant byte
        System.out.println("Original int: " + value);
        System.out.println("Byte array (Big-Endian): " + java.util.Arrays.toString(bytes));
    }
}

Output:

Original int: 305419896
Byte array (Big-Endian): [18, 52, 86, 120]

This manual example produces big-endian order. Notice the difference in byte order compared to the ByteBuffer example.


Converting an Object to a byte[]

You cannot directly convert an arbitrary Java object to a byte array. To do this, the object must be serialized. Serialization is the process of converting an object's state into a byte stream so that it can be saved to a file, sent over a network, or stored in a database.

The object's class must implement the java.io.Serializable marker interface.

import java.io.*;
import java.util.Arrays;
// The class must implement Serializable
class User implements Serializable {
    private static final long serialVersionUID = 1L; // Good practice for versioning
    private String username;
    private transient String password; // 'transient' means this field will NOT be serialized
    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }
    @Override
    public String toString() {
        return "User{username='" + username + "', password='" + password + "'}";
    }
}
public class ObjectSerialization {
    public static void main(String[] args) {
        User user = new User("john_doe", "secret123");
        // 1. Serialize the object to a byte array
        byte[] serializedBytes = serializeObject(user);
        System.out.println("Serialized to " + serializedBytes.length + " bytes.");
        // System.out.println("Raw bytes: " + Arrays.toString(serializedBytes)); // Can be very long
        // 2. Deserialize the byte array back into an object
        User deserializedUser = (User) deserializeObject(serializedBytes);
        System.out.println("Deserialized object: " + deserializedUser);
    }
    // Helper method to serialize an object
    public static byte[] serializeObject(Object obj) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
             ObjectOutputStream oos = new ObjectOutputStream(bos)) {
            oos.writeObject(obj);
            oos.flush();
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    // Helper method to deserialize a byte array
    public static Object deserializeObject(byte[] bytes) {
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
             ObjectInputStream ois = new ObjectInputStream(bis)) {
            return ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Output:

Serialized to 109 bytes.
Deserialized object: User{username='john_doe', password='null'}

Notice that the password field is null because it was marked as transient.


Summary: Which Method to Use?

Your Goal Recommended Method Why?
Convert a String myString.getBytes(StandardCharsets.UTF_8) Explicit, portable, and modern. Avoids platform-specific bugs.
Convert a primitive (int, long, etc.) ByteBuffer.allocate(4).putInt(myInt).array() Clean, safe, handles endianness, and is less error-prone than manual bit-shifting.
Convert an Object ObjectOutputStream / ObjectInputStream This is the standard Java mechanism for object serialization (turning an object into a byte stream).
分享:
扫描分享到社交APP
上一篇
下一篇