简体   繁体   English

将UTF-8字符串转换为字母数字字符串,而不会丢失信息

[英]Convert UTF-8 string to alphanumeric string without information loss

I want to use Jake Wharton's DiskLruCache for Android to cache CouchDb documents on disk. 我想用杰克沃顿DiskLruCache为Android缓存CouchDB的文件在磁盘上。 CouchDb ids are just any JSON String, so could look Sömething/Like/Thís . CouchDb ids只是任何JSON字符串,因此看起来Sömething/Like/Thís However, the library's docs state 但是,该库的文档状态

Each cache entry has a string key and a fixed number of values. 每个高速缓存条目都有一个字符串键和固定数量的值。 Each key must match the regex [a-z0-9_-]{1,64} . 每个密钥必须与正则表达式[a-z0-9_-]{1,64}相匹配。

So I need a way to transform an arbitrary strings to conform to the regex [a-z0-9_-]{1,64} , while still being unique. 因此,我需要一种方法来转换任意字符串以使其符合正则表达式[a-z0-9_-]{1,64} ,同时仍然是唯一的。 How can I do this elegantly? 我该如何优雅地做到这一点?

How about calculating a 64 character hash of the original JSON String and using this hash as a key for the cache? 如何计算原始JSON字符串的64个字符的哈希并将该哈希用作缓存的键?

But, this would not be guaranteed to be unique. 但是,这不能保证是唯一的。 But then again, mapping any JSON String to *[a-z0-9_-]{1,64}* will never be anyways. 但是再说一次,将任何JSON字符串映射到* [a-z0-9 _-] {1,64} *永远都不会。

From this question : you can convert the original string to a string representation of the hexidecimal representation of its bytes. 这个问题出发:您可以将原始字符串转换为其字节的十六进制表示形式的字符串表示形式。

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes("UTF-8")));
}

Although this might produce a String significantly larger than the previous, and may overflow 64-character limit. 尽管这可能会产生比以前大得多的String,并且可能会溢出64个字符的限制。

Using a hash is not feasible if you want to reverse it. 如果要反转哈希,则不可行。 Base 64 doesn't match your requirements, but you can try something similar: Base 64不符合您的要求,但是您可以尝试类似的方法:

Encode each character using only [a-z0-9_-], or more exactly, if the character doesn't match [a-z0-9_], replace it by its unicode value preceeded by -. 仅使用[a-z0-9_-]编码每个字符,或更确切地说,如果该字符与[a-z0-9_]不匹配,则将其替换为以-开头的unicode值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM