请问怎么把String[]对象转成byte[]对象 比如一个String[3]对象里面有23,45,12怎么把这个对象转成byte[]对象,谢谢大家 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你是说吧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 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(); }} 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'+" "); } } } zjsxxww的方法的确很好,学习ing,我想可以把数据先写成等长的再组装就可以知道哪些是一个元素,就可以实现了。不过把23拆成两个元素了,有没有可以不拆分的方法 有byte 能存 23 这个数字吗? 发个简单的题,看看输出是多少 一个计算题 很菜的问题 熟悉使用Eclipse编程环境的帮小弟回答下一个问题...! flash怎么捕获webservice的异常信息。 精英? [100分]为什么在java中如此简单的程序、、、、 byte a=1;byte b=1;short c=a+b; 、、、会报出如下错误!!? 这样用Vector有什么不对?急,在线等.. 谁有实现Java Web服务器的资料。 捐分贴(3) 一个简单的算法问题,谢谢大家指点 怎么样判断分割符个数? 如何把JAVA中的变量传到数据库中???
具体说说吧,谢谢
to wlp555ren
就是你说的那个意思,主要是想看看有没有已经封装好的不用循环的方法
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);
}
}
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();
}
}
{
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'+" ");
}
}
}