输入字符串压缩为字符串
我想将字符串压缩/转换为新字符串。
IE: 输入字符串:
USERNAME/REGISTERID
压缩后的输出字符串:
<some-string-in-UTF8-format>
解压缩后的输出字符串:
USERNAME/REGISTERID
有一些压缩或散列方法用于此转换吗?
我更喜欢使用 Java 的解决方案或具有基本流程步骤的算法。
我已经阅读并尝试使用霍夫曼转换,但压缩输出由字节出站 UTF-8 字符集组成。
I want to compress/transform a string as new string.
i.e.:
input string:
USERNAME/REGISTERID
output string after compress:
<some-string-in-UTF8-format>
output string after decompress:
USERNAME/REGISTERID
There are some compress or hash method for this transformation?
I prefer some solution using Java or an algorithm with basic process steps.
I already read and try to use Huffman transformation, but the compressed output are composed by bytes outbound UTF-8 charset.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 ZipOutputStream 。
您只需要找出正确的字符串编码即可。这种情况可以使用 Base64 表示。
You could use ZipOutputStream.
You just have to figure out the right string encoding. This case be done with a Base64 representation.
看看Base64,commons-codec 等
Commons-code 提供了一个非常简单的 Base64 类 使用。
您不能使用散列函数,因为散列函数通常只能是单向的:即,给定 MD5 或 SHA1 散列,您不应该能够对其进行解码以找出源消息是什么。
Take a look at Base64, commons-codec, etc.
Commons-code provides a very simple Base64 class to use.
You can't use a hash function as hashing functions are typically meant to be one-way only: i.e. given a MD5 or SHA1 hash, you should not be able to decode it to find out what the source message was.
请参阅 iconv 和
mb_convert_encoding
。对于编码,可以考虑base64_encode
。See iconv and
mb_convert_encoding
. For encoding, maybe considerbase64_encode
.如果正如您的名字所示,您的标识符有数据库 ID,为什么不使用这个数字作为编码呢? (如果你愿意,可以将其作为字符串)。
您不应该希望使用压缩算法获得更好的压缩,因为它们都需要一些标头,并且标头本身的大小可能比您的输入字符串长。
if you have database ids for your identifiers as your names suggests, why not using this number as encoding ? (put it as string if you like).
You shouldn't hope to get better compression using compression algorithms as they all need some headers and the header size by itself is probably longer than your input string.
看起来有人要求您混淆用户名/密码组合。这可能不是一个好主意,因为它暗示了没有安全性的地方。您不妨为此实现 ROT13 加密并使用双 ROT13 进行解密。
It looks like someone is asking you to obfuscate username/password combinations. This is probably not a good idea, since it suggests security where there is none. You might as well implement a ROT13 encryption for this and use double ROT13 to decrypt.