[英]Integer to Alphanumeric string conversion
I need to convert five digit integer to alphanumeric string of length 5.Doing below but sometimes it doesn't provide alphanumeric but numeric value. 我需要将五位数的整数转换为长度为5的字母数字字符串。在下面进行操作,但有时它不提供字母数字,而是数字值。
Long x = 12345L;
String code = Long.toHexString(x).toUpperCase();
I want to get Alphanumeric string of length 5 always. 我想总是得到长度为5的字母数字字符串。
That's hardly surprising. 这不足为奇。
For example, 0x12345
is 74565
, so 74565
does not contain any of the digits A
to F
when converted to hexadecimal. 例如,
0x12345
是74565
,因此74565
在转换为十六进制时不包含任何数字 A
到F
Given that 99999
is 0x1869F
, you have plenty of room in your converted string to accommodate some "junk" data, consider introducing an additive constant ( 0xA0000
perhaps which at least guarantees at least one alpha character for positive inputs), or even a number that your XOR with your original. 假设
99999
是0x1869F
,则转换后的字符串中有足够的空间容纳一些“垃圾”数据,请考虑引入一个加法常数( 0xA0000
也许至少可以保证至少一个正数输入的字母字符),甚至是一个数字您与原件的XOR。
Try this 尝试这个
static String alphaNumric(int value) {
String s = "abcde" + Integer.toString(value, 36);
return s.substring(s.length() - 5);
}
and 和
int[] tests = { 12345, 1, 36, 36 * 36, 32767, 99999 };
for (int i : tests)
System.out.println(i + " -> " + alphaNumric(i));
output 输出
12345 -> de9ix
1 -> bcde1
36 -> cde10
1296 -> de100
32767 -> depa7
99999 -> e255r
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.