int[]转换成byte[]在线等??

alengan 2008-04-23 01:29:07
int javaGraphic[] = new int[0x1fa400];怎么样转换成一个byte[].在线等???
...全文
506 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inhibitory 2008-04-30
  • 打赏
  • 举报
回复
public class Test01 {

public static void main(String[] args) {
int length = 0x1fa400;
int[] javaGraphic = new int[length]; // 怎么样转换成一个byte[].在线等???
byte[] bArray = new byte[length * 4];

for (int i = 0, j = 0; i < javaGraphic.length; ++i, j += 4) {
bArray[j + 0] = (byte) (javaGraphic[i] & 0xFF);
bArray[j + 1] = (byte) ((javaGraphic[i] >> 8) & 0xFF);
bArray[j + 2] = (byte) ((javaGraphic[i] >> 16) & 0xFF);
bArray[j + 3] = (byte) ((javaGraphic[i] >> 24) & 0xFF);
// 从byte转回int时, 只要做相应的左移就可以了, 右移24位的, 转回int时, 就左移24位
// 如: int temp = (bArray[j + 3] << 24) | (bArray[j + 2] << 16) | (bArray[j + 1] << 8) | bArray[j + 0]
}
}
}
bootupnow 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 jiangnaisong 的回复:]
引用 12 楼 bootupnow 的回复:
引用 10 楼 jiangnaisong 的回复:
引用 8 楼 bootupnow 的回复:
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!

答:不过writeByte(int i)有可能丢失数据呀,怎么办呢?

如果要用DataInputStream再读还原就有意义。再学习之!

答:如果用:writeByte(0xaabbccdd);则只写了最低的那个dd字节,高三个字节aa,bb,cc都丢了呀,用DataInputStream再读,怎么…
[/Quote]
说的太跳鸟,没集合上下文。
偶的意思就是你的意思,需要还原就是writeInt(int i)、不用的话就凑合writeByte(int i)
连3 学习!
云上飞翔 2008-04-24
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 bootupnow 的回复:]
引用 13 楼 jiangnaisong 的回复:
引用 12 楼 bootupnow 的回复:
引用 10 楼 jiangnaisong 的回复:
引用 8 楼 bootupnow 的回复:
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!

答:不过writeByte(int i)有可能丢失数据呀,怎么办呢?

如果要用DataInputStream再读还原就有意义。再学习之!

答:如果用:writeByte(0xaabbccdd);则只写了最低的那个dd字节,高三个字节aa,bb…
[/Quote]
答:给bootupnow兄弟端杯香茶,倒杯热水。:)

云上飞翔 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 bootupnow 的回复:]
引用 10 楼 jiangnaisong 的回复:
引用 8 楼 bootupnow 的回复:
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!

答:不过writeByte(int i)有可能丢失数据呀,怎么办呢?

如果要用DataInputStream再读还原就有意义。再学习之!
[/Quote]
答:如果用:writeByte(0xaabbccdd);则只写了最低的那个dd字节,高三个字节aa,bb,cc都丢了呀,用DataInputStream再读,怎么还原出丢掉的aa,bb,cc三个字节呢?怎么办呢?
bootupnow 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 jiangnaisong 的回复:]
引用 8 楼 bootupnow 的回复:
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!

答:不过writeByte(int i)有可能丢失数据呀,怎么办呢?
[/Quote]
如果要用DataInputStream再读还原就有意义。再学习之!
云上飞翔 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 alengan 的帖子:]
int javaGraphic[] = new int[0x1fa400];怎么样转换成一个byte[].在线等???
[/Quote]
答:另外,转换成byte[]后,要不要再还原成:int[]?或者说:要不要还能再读出原来正确的int值?
云上飞翔 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 bootupnow 的回复:]
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!
[/Quote]
答:不过writeByte(int i)有可能丢失数据呀,怎么办呢?
胡矣 2008-04-23
  • 打赏
  • 举报
回复
看看
bootupnow 2008-04-23
  • 打赏
  • 举报
回复
4楼用writeInt(int i)会有4个字节呀,writeByte(int i)可以只产生1个byte
学习!
KKK2007 2008-04-23
  • 打赏
  • 举报
回复
我都调试通过了
//转换类,把int[]转化为byte[]
public class IntToByte {
public byte[] toByte(int[] source){//参数为要被转换的int[]的引用
StringBuilder sb=new StringBuilder();
for(int i=0;i<source.length;i++){
sb.append(Integer.toHexString(source[i]));
}
return sb.toString().getBytes();//返回为转换后的byte[]
}
}
//示例用法
public class sample{

public static void main(String[] args) {
int javaGraphic[] = new int[0x1fa400];
IntToByte kk=new IntToByte();//new一个转换类
for(int i=0;i<javaGraphic.length;i++){
javaGraphic[i]=0xff;
}//int[]数组初始化,我是随便写的,你这里要根据需要初始化
byte[] result=kk.toByte(javaGraphic);//把int[]类型的javaGraphic转化为byte[]类型的result
}
}
Netwrom 2008-04-23
  • 打赏
  • 举报
回复
very good
hmsuccess 2008-04-23
  • 打赏
  • 举报
回复
int[]转换成byte[]???///int转换为byte[]
云上飞翔 2008-04-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 alengan 的帖子:]
int javaGraphic[] = new int[0x1fa400];怎么样转换成一个byte[].在线等???
[/Quote]
答:以下代码供你参考:(代码已测试过)

public static byte[] intToByte(int[] ia)throws Exception
{

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dis = new DataOutputStream(baos);
for(int e:ia)
{
dis.writeInt(e);
}
dis.flush();
dis.close();
return baos.toByteArray();
}
anqini 2008-04-23
  • 打赏
  • 举报
回复
这很有可能丢失数据!
haoxiongok 2008-04-23
  • 打赏
  • 举报
回复
楼上的可行,楼主试试
ROBINAPOLLO 2008-04-23
  • 打赏
  • 举报
回复
先把new int[0x1fa400]转成INTEGER[]型
然后通过 Integer.valueOf("FFFF",16).toString())将16进制转成10进制,
然后再通过Integer.toBinaryString(int i),将得到的10进制转化成而进制
也就是楼主要的byte[].了

62,616

社区成员

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

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