Javascript中的位压缩 [英] Bit compression in Javascript

查看:177
本文介绍了Javascript中的位压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法压缩一个JavaScript数组的250 + 1和0到一些更易于管理(比如一个较短的字符串),然后管理解压缩相同?类似于Google对其图像编码的方式...

Is there a way to compress a JavaScript array of 250+ 1s and 0s into something a little more manageable (say a shorter string) and then manageably decompress the same? Sort of like the way Google did its image encodings...

谢谢!

推荐答案

我可以给你几乎1:5压缩通过编码为基础32.我选择包括一个简单的长度值,使它允许可变长度。请参阅这个小提示演示技巧,其中包含两个函数,允许您对值进行往返。 (或者你可以看到一个更早,更幼稚的十六进制版本我之前创建@slebetman提醒我本机号码

I can give you almost 1:5 compression by encoding as base 32. I chose to include a simple length value to make it allow variable-length. Please see this fiddle demonstrating the technique with two functions that allow you to round-trip the value. (Or you can see an earlier, more naive hexadecimal version I created before @slebetman reminded me of the native number base conversion that exists in javascript.)

这里是一组250个1和0的示例输出。字符数不计入前导250 |:

Here's sample output for one set of 250 1s and 0s. The number of characters does not count the leading "250|":

base 32, 50 chars: 250|qgl6alf1q2lbl1aclau3k5ana2kpals78alek59ilboeglajgu
base 16, 63 chars: 250|D42A6555E1D0AABA854CAABC3A155750A995578742AAEA1532AAF0E85553878

您可以使用base 64编码到42个字符,但要注意的是,对于base 32和base 64版本,你可能最终结果中的单词可能是令人反感的(请参阅上面的例子)。十六进制版本也可能有令人反感的内容,但更少的(一个坏的表面,爸爸是一个cad?)

You can use a base 64 encoding to get it down to 42 characters, but be aware that with both the base 32 and base 64 versions you can end up with words in your final result that may be objectionable (please see the fiddle above for an example). The hex version can also have objectionable content, but much less so (a bad face bade a dad be a cad?)

请让我知道,如果你需要保存8更多的字符,我将为您编写额外的脚本。避免元音可能是一种处理令人反感的词问题的方法。

Please let me know if you need to save 8 more characters and I will work up additional script for you. Avoiding vowels could be one way to deal with the objectionable word problem. Let me know if you need to do this as well.

如果你的位串会总是为250个字符,那么函数可以简化有一点,但是我不想做这个假设。

If your bit strings will always be 250 characters, then the functions can be simplified a bit, but I didn't want to make this assumption.

这里是bit-to-base-32函数。

For reference here's the bits-to-base-32 function.

function bitstringEncode(bitstring) {
    var i, l = bitstring.length,
        retval = l.toString() + '|';
    for (i = 0; i < l; i += 5) {
        retval += parseInt((bitstring.substr(i, 5) + '0000').substr(0, 5), 2).toString(32);
    }
    return retval;
}

此函数将填充最接近的5位,字符在您提供的长度的结尾。我包括每个转换函数的第二个版本,填充到最接近的10位,这可能产生多达两个假的额外字符。我包括它们,因为如果速度很重要,他们可能会(或可能不)更快,因为他们从输入中取更大的块。

This function will pad to the nearest 5 bits, and may generate a spurious extra character at the end for the length you are providing. I included a second version of each conversion function that pads to the nearest 10 bits, which may generate up to two spurious extra characters. I included them because if speed is important, they may (or may not) be faster, as they take larger chunks from the inputs.

这篇关于Javascript中的位压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆