javascript - 在 JavaScript 中将二进制字符串压缩/解压缩为十六进制不起作用

标签 javascript binary hex compression

简介

我目前正在用 js 编写 John Conway 的《生命游戏》。我的游戏正在运行( view here ),并且我正在开发额外的功能,例如与您的 friend 分享您的“网格/游戏”。为此,我将网格的值(如果单元格是活的还是死的)提取到一长串 0 和 1 中。

这个长字符串可以看作二进制代码,我试图通过将二进制文件切成长度为 8 的子字符串,然后确定其十六进制值,将其“压缩”为十六进制字符串。解压则相反。将十六进制字符串分成两位并确定其二进制值。

parseInt('00011110', 2).toString(16); // returns '1e'
parseInt('1e', 16).toString(2); // returns '11110'
// Technically both representations still have the same decimal value

如上所示,js 将切断前导 0,因为它们“不需要”。 我通过查看函数返回的二进制字符串的长度是否为 8 来解决这个问题,如果不是,则在前面添加足够的 0,直到其长度恰好为 8。

可能是这个功能无法正常工作,但我不确定。

它似乎适用于较小的二进制值。

请注意,您只能输入长度可被 8 整除的字符串

问题

较长的二进制字符串似乎不起作用(如下所示),这可能不是由溢出引起的(这可能会导致末尾有一长排 0)。

编辑:

var a = "1000011101110101100011000000001011111100111011010011110000000100101000000111111010111111110101100001100101110001100110110101000111110001001010110111001010100011010010111001110010111001101100000100001001101000001010101110001001001110101001110001001111010110011000010100001111000111000011000101010110010011101100000100011101101110110000100101000110011101101011011111010111001001000101000001001111010010010010100000110101101101110101110101010101111101100110101110100100110000010000000110000100000001110001011001011011000101111110101000100011010100011001000101111001000010001011001011100100110001101100001111110110000000111010100101110110101110110111001100000001001100111110000111001010111110110100010111001011101110011011100100111010001100010111100111011010111110111101010000111101010100011000000111000010101011101101011110010011001110000111100000111011111011000000100000010100001111110101001110001100011001"  

a.length  
904  

var c = compress(a)  

c  
 "87758c2fced3c4a07ebfd619719b51f12b72a34b9cb9b042682ae24ea713d66143c7c5593b0476ec2519dadf5c91413d24ad6dd7557d9ae93040611c596c5fa88d4645e422cb931b0fd80ea5daedcc04cf872bed172ee6e4e8c5e76bef5f546070abb5e4ce1eefb25fd4e319"  

var d = decompress(c)

d
"100001110111010110001100001011111100111011010011110001001010000001111110101111111101011000011001011100011001101101010001111100010010101101110010101000110100101110011100101110011011000001000010011010000010101011100010010011101010011100010011110101100110000101000011110001111100010101011001001110110000010001110110111011000010010100011001110110101101111101011100100100010100000100111101001001001010110101101101110101110101010101111101100110101110100100110000010000000110000100011100010110010110110001011111101010001000110101000110010001011110010000100010110010111001001100011011000011111101100000001110101001011101101011101101110011000000010011001111100001110010101111101101000101110010111011100110111001001110100011000101111001110110101111101111010111110101010001100000011100001010101110110101111001001100111000011110111011111011001001011111110101001110001100011001"  

d == a  
false

编辑结束 enter image description here

我的代码

我用来压缩的函数:

function compress(bin) {
  bin = bin.toString(); // To make sure the binary is a string;
  var returnValue = ''; // Empty string to add our data to later on.

  for (var i = 0; i < parseInt(bin.length / 8); i++) {
    // Determining the substring.
    var substring = bin.substr(i*8, 8)
    // Determining the hexValue of this binary substring.
    var hexValue = parseInt(substring, 2).toString(16);
    // Adding this hexValue to the end string which we will return.
    returnValue += hexValue;
  }

  // Returning the to hex compressed string.
  return returnValue;
}

我用来解压的函数:

function decompress(compressed) {
  var returnValue = ''; // Empty string to add our data to later on.

  for (var i = 0; i < parseInt(compressed.length / 2); i++) {
    // Determining the substring.
    var substring = compressed.substr(i*2, 2);
    // Determining the binValue of this hex substring.
    var binValue = parseInt(substring, 16).toString(2);

    // If the length of the binary value is not equal to 8 we add leading 0s (js deletes the leading 0s)
    // For instance the binary number 00011110 is equal to the hex number 1e,
    // but simply running the code above will return 11110. So we have to add the leading 0s back.
    if (binValue.length != 8) {
      // Determining how many 0s to add:
      var diffrence = 8 - binValue.length;

      // Adding the 0s:
      for (var j = 0; j < diffrence; j++) {
        binValue = '0'+binValue;
      }
    }

    // Adding the binValue to the end string which we will return.
    returnValue += binValue
    }
    // Returning the decompressed string.
    return returnValue;
}

有人知道出了什么问题吗?或者如何正确地做到这一点?

最佳答案

问题是您期望压缩函数始终添加 2 个十六进制字母对,但情况并非总是如此。例如,“00000011”仅给出“3”,但您实际上想要“03”。因此,您需要在压缩函数中涵盖这些情况:

var hexValue = parseInt(substring, 2).toString(16);
if(hexValue.length == 1) hexValue = '0'+hexValue

关于javascript - 在 JavaScript 中将二进制字符串压缩/解压缩为十六进制不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38105759/

相关文章:

r - 如何用 1 和 0 覆盖 R 中的一列字符?

PHP 十六进制字符串作为套接字发送

java - Hector 无法正确处理 Java 字符串中的控制字符 - 如何从 Hector 获取十六进制而不是文本字符串?

python - 如何使用 python 查看目录中每个文件的第一行

javascript - 如何在JavaScript中不写 "eval"就执行 "eval"

javascript - jQuery 倒数计时器不倒计时

c - 为什么这个uint32_t的值是-2147483648呢?

c - 使用二元运算计算 n 个 1

javascript - 为什么我只得到最后附加的数据 - 使用 canvasjs

javascript - 如何通过用户(输入字段)更改函数内部全局变量的值