用于我的LZW压缩代码。我选择将代码存储在9位代码中,字典大小将为512,因此只有256个新符号的空间。现在我觉得我没有为工作选择正确的缓冲区:
byte[] buffer = new byte[3];这个缓冲区更适合存储12位,9位的等价物是什么,我如何正确地在缓冲区中存储9位?
我用它在buffer中存储8位,在buffer1中存储4位。9位的等价物是什么?
buffer[0] = (byte) (code & 255);
buffer[1] = (byte) ((code >> 8) << 4);发布于 2019-10-16 13:49:38
9是一个很难处理的位数。第一个问题是:你能用8比特工作吗?
假设没有,我会考虑在字典级别进行分配,并在不考虑字节边界的情况下打包9位单词。一个512字节的字典= 4096位= 455个9位符号。你只需要一些数学运算就可以从你的码流中访问这些符号:
byte[] buffer = new byte[512];
function getWord(int wordOfs) {
// Gets wordOfs'th 9-bit symbol from buffer, For buffer of 512 bytes, wordOfs = 0 -> 454
if (wordOfs<0 || wordOfs>454) throw InvalidArgumentException;
int bitsOfs = wordOfs * 9; // Offset (in bits) of the desired 9 bit word
int idx = bitsOfs / 8; // buffer[idx] contains bit 0 of the desired word
int ofs = bitsOfs % 8; // ... shifted this many places to the right
// Grab at least 8 bits beyond the calculated starting point
unsigned word val = buffer[idx] | (buffer[idx+1]>>8);
// Shift and mask it down to the desired 9 bits for return
return (val << ofs) & 0x01FF;
}注意:我现在还不能使用Java编译器,语法可能需要改进。
https://stackoverflow.com/questions/58405470
复制相似问题