请问怎么把String[]对象转成byte[]对象

xb3dcs 2007-01-10 04:06:12
比如一个String[3]对象
里面有23,45,12
怎么把这个对象转成byte[]对象,谢谢大家
...全文
368 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbhbhbhbhb1021 2007-01-11
  • 打赏
  • 举报
回复
zjsxxww的方法的确很好,学习ing,我想可以把数据先写成等长的再组装就可以知道哪些是一个元素,就可以实现了。不过把23拆成两个元素了,有没有可以不拆分的方法
wangpingsx 2007-01-11
  • 打赏
  • 举报
回复
有byte 能存 23 这个数字吗?
biaoflying 2007-01-10
  • 打赏
  • 举报
回复
顶楼上的 我怎么就没有想到呢?
zjsxxww 2007-01-10
  • 打赏
  • 举报
回复
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'+" ");
}
}
}
biaoflying 2007-01-10
  • 打赏
  • 举报
回复
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();
}
}
biaoflying 2007-01-10
  • 打赏
  • 举报
回复
不知道这样可以不?
xlbc 2007-01-10
  • 打赏
  • 举报
回复
用ByteArrayInputStream 或 ByteArrayOutputStream 串接到 InputStreamReader 或 OutputStreamWriter
biaoflying 2007-01-10
  • 打赏
  • 举报
回复
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);
}
}
hbhbhbhbhb1021 2007-01-10
  • 打赏
  • 举报
回复
to fool_leave
具体说说吧,谢谢
to wlp555ren
就是你说的那个意思,主要是想看看有没有已经封装好的不用循环的方法
wlp555ren 2007-01-10
  • 打赏
  • 举报
回复
对象转换?你就把String对象里的值取出来,再附给Byte对象不行吗?
fool_leave 2007-01-10
  • 打赏
  • 举报
回复
你是说吧String[]看成一个整体来转换?

如果这样,就用ObjectOutputStream封装ByteArrayOutputStream,然后将这个对象转换
xingyue2003 2007-01-10
  • 打赏
  • 举报
回复
具体点可以吗?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧