[英]How to convert a byte array to a String in Java, and skip all null bytes?
[英]How to convert the String array into bytes?
我有一個數組字符串,我需要轉換為字節,那么我該怎么做呢?
樣品是
String test [] = {“Hello”,“Hello1”};
我需要在一個地方將它們轉換為字節,並將它們作為字符串返回到其他地方。 我需要以字節形式傳入,我有字符串數組,這是這樣的嗎?還是其他任何替代方法?
您可以通過迭代數組並使用來輕松地將String[]
轉換為byte[][]
bytes[i] = str[i].getBytes(encoding);
String Convertthis = "i want to convert this string to byte array"
byte[] theByteArray = Convertthis.getBytes("UTF8");
System.out.println(theByteArray.length);
請參閱http://javasourceblog.blogspot.in/2012/03/array-in-java.html
迭代String
數組,將每個String
值附加到StringBuilder
對象。 完成迭代后,返回`builder.toString.getBytes()
public byte[] stringToByte(String[] str){
StringBuilder builder = new StringBuilder();
for(int i=0;i<str.length;i++){
builder.append(str[i]);
}
return builder.toString().getBytes("UTF8");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.