如何创建表示 Java long 的字符串,就好像它是无符号 64 位值一样
我的组件收到一个长值,稍后我将其用作缓存的键。 键本身是长值的字符串表示形式,就好像它是无符号 64 位值一样。 也就是说,当我的组件被传递到 -2944827264075010823L 时,我需要将其转换为字符串键“15501916809634540793”。
我有一个解决方案,但它似乎很暴力,这让我有点恶心。 本质上,我将 long 转换为十六进制字符串表示形式(因此 -2944827264075010823L 变为“d721df34a7ec6cf9”)并将十六进制字符串转换为 BigInteger:
String longValueAsHexString = convertLongToHexString(longValue);
BigInteger bi = new BigInteger(longValueAsHexString, 16);
String longValueString = bi.toString();
然后使用 longValueString 作为缓存中的键。
我无法使用 Long.toString(longValue,16),因为它返回绝对值的十六进制字符串,前缀为“-”。
所以我的 ConvertLongToHexString 看起来像这样:
long mask = 0x00000000ffffffffL;
long bottomHalf = number & mask;
long upperHalf = (number >> 32) & mask;
String bottomHalfString = Long.toString(bottomHalf, 16);
if (bottomHalfString.length() != 8) {
String zeroes = "0000000000000000";
bottomHalfString = zeroes.substring(16-bottomHalfString.length()) + bottomHalfString;
}
return Long.toString(upperHalf,16)+bottomHalfString;
必须有一种更优雅的方法来做到这一点。 有什么建议么?
My component is handed a long value that I later use as a key into a cache. The key itself is a string representation of the long value as if it were unsigned 64-bit value. That is, when my component is handed -2944827264075010823L, I need to convert that into the string key "15501916809634540793".
I have a solution, but it seems brute force and it makes me a bit queasy. Essentially, I convert the long into a hexadecimal string representation (so -2944827264075010823L becomes "d721df34a7ec6cf9") and convert the hexadecimal string into a BigInteger:
String longValueAsHexString = convertLongToHexString(longValue);
BigInteger bi = new BigInteger(longValueAsHexString, 16);
String longValueString = bi.toString();
I then use longValueString as the key into the cache.
I cannot use Long.toString(longValue,16), because it returns the hex string for the absolute value, prefixed by a "-".
So my convertLongToHexString looks like this:
long mask = 0x00000000ffffffffL;
long bottomHalf = number & mask;
long upperHalf = (number >> 32) & mask;
String bottomHalfString = Long.toString(bottomHalf, 16);
if (bottomHalfString.length() != 8) {
String zeroes = "0000000000000000";
bottomHalfString = zeroes.substring(16-bottomHalfString.length()) + bottomHalfString;
}
return Long.toString(upperHalf,16)+bottomHalfString;
There must be a more elegant way of doing this. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我的实现。 我对其进行了重构,使其具有一个采用
long
并返回字符串的函数。 :-)Here's my implementation. I've refactored it to have a function taking a
long
and returning a string. :-)我认为克里斯的答案更好,但这是另一个只是为了好玩。
I think Chris's answer is better, but here's another just for fun.
晚了五年,但这里是一个不使用 BigInteger 或字节数组的实现。
相反,它模拟一步的无符号除法,并将其余部分卸载到标准库函数:
Five years late, but here is an implementation that doesn't use
BigInteger
or byte arrays.Instead, it emulates unsigned division for one step and offloads the rest to the standard library function:
无位实现:
问候,Alex
Bitless implementations:
regards, Alex