如何将整数编码为其他整数?

时间:2018-07-07 22:09:23

标签: encoding integer byte bitstream

作为对Store 2 4-bit numbers in 1 8 bit number的跟进,我想知道是否有一个概括,可以将n个x位数字存储为m个y位数字。例如,也许您可​​以将5个8位数字存储为3个15位数字。或者将2个8位数字转换为1个16位数字,或将3个16位数字转换为2个32位数字。想知道执行此操作的过程的编码和解码将实现什么,或者如果不可能的话。

类似的东西:

function encode(i, s1, n, s2) {
  // i = array of input bytes
  // s1 = size of input bytes
  // n = number of output bytes
  // s2 = size of output bytes
}

function decode(i, s1, n, s2) {

}

基于以下答案,我尝试将其翻译为JavaScript,但不了解任何真正含义,也不认为它有用。

function encode(input, inputSize, outputSize, callback) {
  var buffer = 0
  var bbits = 0
  var mask = (1 << outputSize) - 1
  while (bbits < outputSize) {
    buffer |= (input << bbits)
    bbits += inputSize
  }
  while (bbits >= outputSize) {
    callback(buffer & mask)
    buffer >>= outputSize
    bbits -= outputSize
  }
}

2 个答案:

答案 0 :(得分:3)

您不能将5个8位数字存储为3个15位数字,因为45位信息显然不适合40位内存。仅当变体总数小于或等于2 k 时才可以这样做,其中k是用于编码的位数

如果每个值的宽度都相同,则这是我的尝试,该方法将位线性存储在big endian中。编码函数将字节数组中的位转换为另一个数组,该数组将完整值存储在bitLength位中,而解码函数则执行相反的操作

function encode(input, bitLength) {
  // size of each array element must be greater than bitLength
  var output = new Uint16Array(Math.ceil(input.length * 8 / bitLength));
  var remainingBits = bitLength; // the remaining bits left for the current value

  // example when bitLength = 11
  //       start of current value
  //       │          next value
  //       │2345678901│
  // ...┆  ↓    ┆     ↓ ┆       ┆       ┆       ┆...      ← input bytes
  // ...₀₁₂₃₄₅₆₇⁰¹²³⁴⁵⁶⁷₀₁₂₃₄₅₆₇⁰¹²³⁴⁵⁶⁷₀₁₂₃₄₅₆₇ ...      ← bit position

  for (var inIdx = 0, outIdx = 0; inIdx < input.length; inIdx++) {
    if (remainingBits > 8) {
      output[outIdx] = (output[outIdx] << 8) | input[inIdx];
      remainingBits -= 8;               // 8 less bits to read
    } else if (remainingBits == 8) {    // finish current value
      output[outIdx] = (output[outIdx] << 8) | input[inIdx];
      remainingBits = bitLength; // next byte is the start of the next output value
      outIdx++;
    } else {
      var nextRemainingBits = 8 - remainingBits;
      output[outIdx] = (output[outIdx] << remainingBits)
                     | (input[inIdx] >>> nextRemainingBits);
      // the leftover bits (nextRemainingBits) in the input byte
      // go into the next output
      output[++outIdx] = input[inIdx] & ((1 << nextRemainingBits) - 1);
      // adjust the number of remaining bits, after we've read
      // `8 - remainingBits` bits for the current output
      remainingBits = bitLength - nextRemainingBits;
    }
  }
  return output;
}

function decode(input, bitLength) {
    const numBits = input.BYTES_PER_ELEMENT*8;
  var output = new Uint8Array(Math.ceil(input.length * bitLength / 8));
  var remainingInputBits = bitLength; // the remaining bits left for the current value
  
  // shift value to the most significant position
  for (var i = 0; i < input.length; i++)
    input[i] <<= numBits - bitLength;
  
  for (var inIdx = 0, outIdx = 0; outIdx < output.length; outIdx++) {
    if (remainingInputBits > 8) {
      output[outIdx] = input[inIdx] >>> (numBits - 8);  // get the top byte from input
      input[inIdx] <<= 8;   // shift the read bits out, leaving next bits on top
      remainingInputBits -= 8;
    } else if (remainingInputBits == 8) {
      output[outIdx] = input[inIdx] >>> (numBits - 8);
      remainingInputBits = bitLength;
      inIdx++;
    } else {
      remainingInputBits = 8 - remainingInputBits;
      output[outIdx] = input[inIdx] >>> (numBits - 8);
      inIdx++;
      output[outIdx] |= input[inIdx] >>> (numBits - remainingInputBits);
      input[inIdx] <<= remainingInputBits;
      remainingInputBits = bitLength - remainingInputBits;
    }
  }
  return output;
}

function pad(s, size) {
  s = (s >>> 0).toString(2);
  while (s.length < (size || 2)) { s = "0" + s; }
  return s;
}

function printBinaryArray(arr, padLength) {
    var str = "";
    for (var i = 0; i < arr.length; i++)
        str += pad(arr[i], padLength) + " ";
    console.log(str);
}

var inputBytes = 22;
var bitLength = 11; // each value is 11-bit long
var input = new Uint8Array(inputBytes);

window.crypto.getRandomValues(input);

var encodedData = encode(input, bitLength);
console.log("Input data", input);
printBinaryArray(input, 8);
console.log("Encoded data");
// console.log(encodedData);
printBinaryArray(encodedData, bitLength);

var decodedData = decode(encodedData, bitLength);
console.log("Decoded data", decodedData);
printBinaryArray(decodedData, 8);

for (var i = 0; i < input.length; i++)
    if (input[i] != decodedData[i])
        console.log("Wrong decoded data");
console.log("Data decoded successfully");

实际上,编码和解码过程只是彼此相反,因此您可以轻松地将它们修改为encode(input, inputBitWidth, outputBitWidth),可用于编码和解码,只需交换输入和输出宽度

但是,对于奇数大小的值,通常最好将高位打包在一起以便于访问。例如10位像素格式通常将4个像素打包成一个5字节的组,每个像素的8个高位在前4个字节中,最后一个字节包含2个低位

另请参见

答案 1 :(得分:2)

一般情况下是“流式传输”,无论所有内容的对齐方式有多严重,它都有效。像往常一样,它通过降低效率来弥补普遍性。基本上,它的工作原理是将输入拖放到缓冲区中,直到至少可以从中提取一次输出块,然后提取所有输出,因此是这样的:

buffer = 0
bbits = 0
mask = (1 << outSize) - 1
while more input:
    while bbits < outSize:
        buffer |= in() << bbits
        bbits += inSize
    while bbits >= outSize:
        out(buffer & mask)
        buffer >>= outSize
        bbits -= outSize
if bbits != 0:
    out(buffer & mask)

编码和解码在概念上是相同的,但是交换了大小。当专门用于特定大小的输入和输出块时,内部循环之一将不是循环。也可以使用其他打包顺序,将输入块的高位输出到低位之前,随便哪个。

缓冲区的大小必须至少为outSize - 1 + inSize,以容纳从缓冲区输出后剩下的最大位数后的读取输入。

在操作过程中甚至可以更改大小。