inputStream = new DataInputStream(new BufferedInputStream(new FileInputStream(srcFile), 1024 * 8));
byte[] vb = new byte[7];
if (inputStream.read(vb, 0, 6) > 0) {
for (int i = 0; i < 6; i++) {
vb[i] = (byte) ((0xFF & vb[i]) >> 4 | (0xFF & vb[i]) << 4);
vb[i] = (byte) ~vb[i];
}
vb[6] = 0;
}
version = new String(vb, 0, vb.length - 1);
def decode():
version_byte = b'\x9a\xac\x1d\xfc\xfc\xfc'
result_bytes = bytearray()
for i, value in enumerate(version_byte):
version_int = value
version_int = (version_int >> 4 | version_int << 4)
version_int = ~version_int
print(f'{version_int} {version_int}')
result_bytes.insert(i, version_int)
帮忙看看 java 的代码, 我用 python 实现不了同样的功能
1
2i2Re2PLMaDnghL 2021-09-16 12:31:03 +08:00 1
int 和 byte 的区别,Python 我记得应该是没有 byte 类型?至少 bytes.__getitem__ 出来的是 int
version << 4 会超过 byte 范围,做一下 (...)%256 ~version 会变负的,方便点就是 255-version,正规点就是 (~version)%256 |
2
ruanimal 2021-09-16 21:40:32 +08:00 1
处理下溢出
|
3
wangxn 2021-09-16 22:16:13 +08:00 1
需要对位操作的结果作截断避免溢出:
``` def decode(): version_byte = b'\x9a\xac\x1d\xfc\xfc\xfc' result_bytes = bytearray() for i, value in enumerate(version_byte): version_int = value version_int = (version_int >> 4 | version_int << 4) & 0xff version_int = (~version_int) & 0xff print(f'{version_int} {version_int}') result_bytes.insert(i, version_int) ``` |