我想將字符串轉換爲具有相同內容的字節[]。示例 我有:如何將字符串轉換爲具有相同內容的字節[]
String str = "abc";
byte[] bytes;
//I want to convert "str" to "bytes" that they have same content:
(code here)
//after, print bytes -> "abc".
我想將字符串轉換爲具有相同內容的字節[]。示例 我有:如何將字符串轉換爲具有相同內容的字節[]
String str = "abc";
byte[] bytes;
//I want to convert "str" to "bytes" that they have same content:
(code here)
//after, print bytes -> "abc".
只要付出一點努力,您就會達到此目的。
所以我們要做的是使用getBytes
方法
byte[] convertToBytes= stuff.getBytes("UTF-8");
String newString = new String(convertToBytes, "UTF-8");
我想他們有相同的內容,當使用getBytes()時,它的值不同 – CauBeRong
String str = "abc";
byte bytes[] = str.getBytes(); // Get the byte array
for (byte b : bytes) {
System.out.println("Byte is "+b); //Iterate and print
}
str = new String(bytes); // Create String from byte array
System.out.println("String is "+str);
字節數組rght – Kick
我嘗試使用的getBytes,但它是錯的。 – CauBeRong