将一系列 1 和 0 压缩为尽可能短的 ascii 字符串
如何将一系列 1
和 0
转换为由 URL 安全 ascii 字符组成的最短形式?
例如。
s = '00100101000101111010101'
compress(s)
结果如下:
Ysi8aaU
显然:
decompress(compress(s)) == s
(我问这个问题纯粹是出于好奇)
How could you convert a series of 1
s and 0
s into the shortest possible form consisting of URL safe ascii characters?
eg.
s = '00100101000101111010101'
compress(s)
Resulting in something like:
Ysi8aaU
And obviously:
decompress(compress(s)) == s
(I ask this question purely out of curiousity)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我想出的解决方案(+太多评论):
所以:
这是在 CoffeeScript 中:
Here's the solution I came up with (+ far too many comments):
So:
And here it is in CoffeeScript:
正如其中一条评论提到的,使用 base64 可能是最好的选择。但是,您不想在不进行转换的情况下将二进制文件粘贴进去。
两个选项是先转换为 int 然后打包:
另一个选项是使用 struct 模块将值打包为二进制格式并使用它。 (下面的代码来自 http://www.fuyun.org/2009/10/how-to-convert-an-integer-to-base64-in-python/)
As one of the comments mentioned, using base64 would probably be the way to go. However, you don't want to stick the binary in without some converting.
Two options are converting to int first then packing:
The other option would be to use the struct module to pack the value into a binary format and use this. (The code below is from http://www.fuyun.org/2009/10/how-to-convert-an-integer-to-base64-in-python/)
我将使用查找表将其中的 8 个 0 和 1 转换为字节,然后使用 base64 对这些字节进行编码。
I would convert 8 of those 0's and 1's to bytes using a lookup table and then encode those bytes with base64.