日期:2014-05-20  浏览次数:20557 次

请问怎么把String[]对象转成byte[]对象
比如一个String[3]对象
里面有23,45,12
怎么把这个对象转成byte[]对象,谢谢大家

------解决方案--------------------
具体点可以吗?
------解决方案--------------------
你是说吧String[]看成一个整体来转换?

如果这样,就用ObjectOutputStream封装ByteArrayOutputStream,然后将这个对象转换
------解决方案--------------------
对象转换?你就把String对象里的值取出来,再附给Byte对象不行吗?
------解决方案--------------------
to fool_leave
具体说说吧,谢谢
to wlp555ren
就是你说的那个意思,主要是想看看有没有已经封装好的不用循环的方法
------解决方案--------------------
import java.util.*;

public class ParseByte{
public static List makeList(byte[] bytes){
List <Byte> al=new ArrayList <Byte> ();
for(int i=0;i <bytes.length;i++) al.add(bytes[i]);
return al;
}
public static void main(String[] args){
String[] strs={ "23 ", "45 ", "12 "};
ArrayList <Byte> list=new ArrayList <Byte> ();
for(int i=0;i <strs.length;i++)
list.addAll(makeList(strs[i].getBytes()));
Byte[] bytes=(Byte[])list.toArray();
//System.out.println(bytes);
}
}
------解决方案--------------------
用ByteArrayInputStream 或 ByteArrayOutputStream 串接到 InputStreamReader 或 OutputStreamWriter
------解决方案--------------------
public class StringTest
{
public static void main(String[] args)
{
String[] str={ "23 ", "45 ", "12 "};
String str1= " ";
byte[] b;
for(int i=0;i <str.length;i++)
{
str1=str1+str[i];
}
b=str1.getBytes();
for(int i=0;i <b.length;i++)
{
System.out.print(b[i]- '0 '+ " ");
}
}
}
------解决方案--------------------
有byte 能存 23 这个数字吗?