Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试将 8 个整数(0 或 1)的数组转换为 java 字节类型。
例如
[1,0,1,0,0,0,1,0] = 10100010
我尝试在线搜索,也许我没有输入正确的查询?提前致谢!
用于特定索引读取数组BitSet的set()真/假,然后将其转换为字节数组
BitSet
set()
你可以试试这个
byte[] bits = { 1, 0, 1, 0, 0, 0, 1, 0 }; byte b = 0; for (int i = 0, m = 0x80; i < 8; i++, m >>= 1) { if (bits[i] == 1) b |= m; }
或这一行解决方案
byte b = (byte) Integer.parseInt(Arrays.toString(bits).toString().replaceAll("\\D", ""), 2);