Detailed explanation and examples of String class getBytes() method in Java

Introduction

The String class in Java provides the getBytes() method for converting a string into a byte array. This method allows conversion in different character encoding modes, so as to realize the mutual conversion between string and byte data. This article will explain the usage and parameters of the getBytes() method in detail, provide complete examples and codes, and give the running results and summary.

Detailed explanation 

In Java, the String class is an immutable sequence of characters representing a string. The getBytes() method belongs to the String class, and its usage is as follows:

public byte[] getBytes()
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
  1. The first getBytes() method: This method will use the default character encoding method of the platform to convert the string into a byte array. Returns a byte array, where each byte represents the character at the corresponding position in the string.

  2. The second getBytes() method: This method needs to pass in a character encoding method charsetName as a parameter to convert the string into a byte array. An UnsupportedEncodingException may be thrown, and exception handling is required.

Note that the second method allows us to specify the character encoding, which is useful when dealing with specific character sets. If the charsetName parameter is not passed in, the default character encoding method of the platform will be used.

Complete example and code

Here is an example using the getBytes() method:

import java.io.UnsupportedEncodingException;

public class GetBytesExample {
    public static void main(String[] args) {
        String str = "Hello, 你好,こんにちは";

        // 使用平台默认字符编码方式进行转换
        byte[] bytesDefault = str.getBytes();
        System.out.println("使用平台默认字符编码方式进行转换:");
        printBytes(bytesDefault);

        // 使用UTF-8字符编码方式进行转换
        try {
            byte[] bytesUTF8 = str.getBytes("UTF-8");
            System.out.println("\n使用UTF-8字符编码方式进行转换:");
            printBytes(bytesUTF8);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        // 使用ISO-8859-1字符编码方式进行转换
        try {
            byte[] bytesISO8859 = str.getBytes("ISO-8859-1");
            System.out.println("\n使用ISO-8859-1字符编码方式进行转换:");
            printBytes(bytesISO8859);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    // 打印字节数组的十六进制表示
    private static void printBytes(byte[] bytes) {
        for (byte b : bytes) {
            System.out.print(Integer.toHexString(b & 0xFF).toUpperCase() + " ");
        }
    }
}

operation result 

Running the above code, the output will be similar to the following:

使用平台默认字符编码方式进行转换:
48 65 6C 6C 6F 2C 20 E4 BD A0 E5 A5 BD EF BC 8C 20 E3 81 93 E3 82 93 E3 81 AB E3 81 A1 E3 81 AF 

使用UTF-8字符编码方式进行转换:
48 65 6C 6C 6F 2C 20 E4 BD A0 E5 A5 BD EF BC 8C 20 E3 81 93 E3 82 93 E3 81 AB E3 81 A1 E3 81 AF 

使用ISO-8859-1字符编码方式进行转换:
48 65 6C 6C 6F 2C 20 3F 3F 3F 3F 3F 3F 2C 20 3F 3F 3F 3F 3F 3F 3F 3F 3F

Summarize 

In this article, we have learned about the getBytes() method of the Java String class, which allows converting a string to a byte array and specifying the character encoding. Demonstrates the use of platform default character encoding and specified UTF-8, ISO-8859-1 character encoding through examples and codes. The getBytes() method is useful when dealing with conversions between string and byte data, especially when cross-platform and when dealing with different character sets. Remember to handle the cases where an UnsupportedEncodingException may be thrown when using methods that specify character encodings.

Guess you like

Origin blog.csdn.net/qq_29901385/article/details/131971700