0

I have a hash whose values are set/unset based on some condition(essentially a bitmap). This hash needs to be stored as a string(in a blob) in the most optimal way possible. I figured I could use a bit array and pack them into a number and store this number as a string. Any ideas on how I can go about doing this? Is there a better way to do this?

4

4 回答 4

3

你可以只使用一个BigInteger. 它有一个以字节数组为参数的构造函数,以及一个.toByteArray()将数字作为字节数组获取的构造函数:

final BigInteger myHash = new BigInteger(theByteArray);
final byte[] theArray = myHash.toByteArray();
于 2013-06-04T15:42:35.780 回答
0

我会使用两个long值或四个int值。你不会比这更有效率。如果您需要在任何情况下转换为/从 byte[] 转换,则可以使用 byte[]。

于 2013-06-04T18:07:36.680 回答
0

如果您需要将其存储为字符串,您可能应该对数字进行 base 64 编码。您可以使用 Apache Commons Codec 中的Base64 类。例如

String encoded = Base64.encodeBase64String(myBinaryData);
于 2013-06-04T15:45:49.290 回答
-2

128 位数组只是一个无符号长整数。非常容易和自然的包装。

于 2013-06-04T15:38:35.750 回答