字符串getBytes()方法 (String getBytes() Method)
getBytes() method is a String method in java and it is used to get the byte values of a given string.
getBytes()方法是Java中的String方法,用于获取给定字符串的字节值。
Syntax:
句法:
byte[] String.getBytes();
It accepts String and returns byte array.
它接受String并返回字节数组。
Example:
例:
Input: "IncludeHelp"
Output:
73
110
99
108
117
100
101
72
101
108
112
Code:
码:
public class Main
{
public static void main(String[] args) {
try{
String str = "IncludeHelp";
byte[] arr = str.getBytes();
//printing byte array (each elements in nubers)
System.out.println("Byte values of each character...");
for(int i =0; i<str.length(); i++)
System.out.println("arr[" + i +"] = " + arr[i]);
}
catch(Exception ex){
System.out.println("Error: " + ex.toString());
}
}
}
Output
输出量
Byte values of each character...
arr[0] = 73
arr[1] = 110
arr[2] = 99
arr[3] = 108
arr[4] = 117
arr[5] = 100
arr[6] = 101
arr[7] = 72
arr[8] = 101
arr[9] = 108
arr[10] = 112
翻译自: https://www.includehelp.com/java/string-getBytes-method-with-example.aspx