在javascript中将二进制字符串压缩/解压缩为十六进制不能正常工作

时间:2016-06-29 16:52:57

标签: javascript binary hex compression

简介

我目前正在研究John Conway在js中的生命游戏。我让游戏工作(view here),我正在研究额外的功能,例如与朋友分享你的“网格/游戏”。要做到这一点,我将网格的值(如果单元格活着或死亡)提取为一个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;
}

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

1 个答案:

答案 0 :(得分:1)

问题是你期望你的压缩函数总是添加2对六进制字母对,但情况并非总是如此。例如&#39; 00000011&#39;只给出了一个&#39; 3,但你真的想要&#39; 03&#39;。因此,您需要在压缩函数中涵盖这些情况:

if(!$result2 = $db->query($sql)) die('There was an error Processing Order Again [' . $db->error . ']');
相关问题