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

解决方案 »

  1.   

    你是说吧String[]看成一个整体来转换?如果这样,就用ObjectOutputStream封装ByteArrayOutputStream,然后将这个对象转换
      

  2.   

    对象转换?你就把String对象里的值取出来,再附给Byte对象不行吗?
      

  3.   

    to fool_leave
    具体说说吧,谢谢
    to wlp555ren
    就是你说的那个意思,主要是想看看有没有已经封装好的不用循环的方法
      

  4.   

    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);
    }
    }
      

  5.   

    用ByteArrayInputStream 或 ByteArrayOutputStream 串接到 InputStreamReader 或 OutputStreamWriter
      

  6.   

    import java.io.*;public class ParseByte1{
    public static void main(String[] args)throws IOException{
    String[] strs={"23","45","12"};
    ByteArrayOutputStream bout=new ByteArrayOutputStream();
    ObjectOutputStream out=new ObjectOutputStream(bout);
    out.writeObject(strs);
    byte[] bytes=bout.toByteArray();
    }
    }
      

  7.   

    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'+" ");
            }
    }
    }
      

  8.   

    zjsxxww的方法的确很好,学习ing,我想可以把数据先写成等长的再组装就可以知道哪些是一个元素,就可以实现了。不过把23拆成两个元素了,有没有可以不拆分的方法
      

  9.   

    有byte 能存 23 这个数字吗?