在 Java 中使用一组固定字母将 long 编码/解码为字符串
给定一组任意字母,
String range = "0123456789abcdefghijklmnopABCD#";
我正在寻找两种方法来对长 <-> 进行编码/解码String
String s = encode( range, l );
等
long l = decode( range, s );
decode(range,encode(range, 123456789L)) == 123456789L
如果 range 是“0123456789”,那就是通常的编码方式。
Given an arbitrary set of letters
String range = "0123456789abcdefghijklmnopABCD#";
I am looking for 2 methods to encode/decode from long <-> String
String s = encode( range, l );
and
long l = decode( range, s );
So decode(range, encode(range, 123456789L)) == 123456789L
And if range is "0123456789" thats the usual way of encoding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下代码可以满足您的需要:
请注意,这本质上是使用一组自定义符号的基本转换。
相关问题
The following code does what you need:
Note that this is essentially base conversion with a custom set of symbols.
Related questions
这只是执行基数转换的问题。只需将 long 转换为相应的数字基数(与字符串中的字符数相对应),然后使用范围字符串作为“数字”集。
例如,假设您有字符串“0123456789ABCDEF”,那么这意味着您必须转换为以 16 为基数的十六进制。如果字符串是“01234567”,则转换为以 8 为基数的八进制。
要返回,请获取第一个字符,找到它在字符串中的位置,并将其添加到结果中。然后,对于每个后续字符,将当前结果乘以基数,然后添加下一个字符的位置。
This is simply a matter of performing base conversion. Simply convert the long to the appropriate numeric base, corresponding to the number of characters in your string, and use the range string as your set of "digits".
For example, suppose you have the string "0123456789ABCDEF", then this means you must convert to base 16, hexadecimal. If the string is "01234567", then you convert to base 8, octal.
For going back, take the first character, find its position in the string, and add it to the result. Then, for each subsequent character, multiply the current result by the base before adding the position of the next character.
寻找彭定康和匹配者。这是我的片段
private static Final String LUCENE_ENCODE_ESCAPE_CHARS = "[\\+\-\!\(\)\:\^\]\{\}\~\*\?]";
look for Patten and matcher. Here is my snippet
private static final String LUCENE_ENCODE_ESCAPE_CHARS = "[\\+\-\!\(\)\:\^\]\{\}\~\*\?]";
该编码器将确保任意数量的符号具有相同的长度结果。
测试用例:
结果:
This encoder will ensure the same length result for any number of symbols.
Test case:
Result: