对byte[] 二进制操作

babawoo 2010-03-18 09:10:19
a、b、c 不是1就是0;例如:int a = 1;int b = 0;int c = 1;
byte s[] = new byte[2];因为:1 byte = 8 bit 所以初始化为0000000000000000(二进制16个0)
现在将a、b、c 三个整数转换成如下二进制: 0000000000000abc 写入s,请问如何写方法?
...全文
119 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
l3751202 2010-03-18
  • 打赏
  • 举报
回复

String s[] = new String[2];
byte b[] = new byte[2];
b[0] = Byte.valueOf(s[0], 2);
b[1] = Byte.valueOf(s[1], 2);


没测试。不知道行不行。
babawoo 2010-03-18
  • 打赏
  • 举报
回复
谢谢,那如何写一个方法呢?对byte[]中的二进制中的某一位进行操作。
Dazzlingwinter 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 breezehou 的回复:]

引用 1 楼 dazzlingwinter 的回复:
Java code

String s[] = new String[3];
s[0] = Integer.toBinaryString(a);
s[1] = Integer.toBinaryString(b);
s[2] = Integer.toBinaryString(c);
//多看API呀

数组越界了。
[/Quote]
随便这么一敲就贴了,没在IDE里边运行~,O(∩_∩)O~
breezehou 2010-03-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 dazzlingwinter 的回复:]
Java code

String s[] = new String[2];
s[0] = Integer.toBinaryString(a);
s[1] = Integer.toBinaryString(b);
s[2] = Integer.toBinaryString(c);
//多看API呀
[/Quote]
数组越界了。
Dazzlingwinter 2010-03-18
  • 打赏
  • 举报
回复

String s[] = new String[2];
s[0] = Integer.toBinaryString(a);
s[1] = Integer.toBinaryString(b);
s[2] = Integer.toBinaryString(c);
//多看API呀

62,614

社区成员

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

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