【问题标题】:Compresing / decompresing a binary string into/from hex in javascript not working在javascript中将二进制字符串压缩/解压缩到/从十六进制不起作用
【发布时间】:2016-11-01 12:36:19
【问题描述】:

简介

我目前正在研究 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

编辑结束

我的代码

我用来压缩的函数:

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;
}

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

【问题讨论】:

    标签: javascript binary hex compression


    【解决方案1】:

    问题是您希望您的 compress 函数总是添加成对的 2 个十六进制字母,但情况并非总是如此。例如'00000011' 只给出'3',但你实际上想要'03'。因此,您需要在 compress 函数中涵盖这些情况:

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

    【讨论】:

      猜你喜欢
      • 2012-07-31
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 2020-06-01
      • 2017-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多