How to convert a Java string to a byte type?
In Java, the getBytes() method can be used to convert a string into a byte array. There are multiple overloaded versions of the getBytes() method, allowing you to specify the character encoding format.
The example code is as follows:
String str = "Hello, World!";
byte[] bytes = str.getBytes(); // 使用默认字符编码格式转换为字节数组
// 指定字符编码格式转换为字节数组
byte[] bytesUTF8 = str.getBytes("UTF-8");
byte[] bytesGBK = str.getBytes("GBK");
Please ensure to consider the character encoding format when converting a string to a byte array. If the character encoding format is not specified, the default one will be used.