3 回答

TA貢獻1805條經驗 獲得超10個贊
好消息!Java 提供了任意精度的整數類型。該BigInteger(String, int)構造可以用來把你的十六進制,并進行128位的值。進一步BigInteger是Comparable。你可以像這樣使用它
BigInteger bi = new BigInteger("9c1f03a0d9cf510f2765bd0f226ff5dc", 16);
BigInteger bi2 = bi.add(BigInteger.ONE);
if (bi2.compareTo(bi) > 0) {
System.out.println("Like this");
}
輸出
Like this

TA貢獻1859條經驗 獲得超6個贊
你可以使用BigInteger。
String hexString = "9c1f03a0d9cf510f2765bd0f226ff5dc";
BigInteger bigInt = new BigInteger(hexString, 16);
System.out.println(bigInt);

TA貢獻1816條經驗 獲得超4個贊
使用Long.compareUnsigned(以及其他將longs 視為無符號的方法),小技巧不再是必不可少的。您可以只實施標準的多元素比較,首先處理更重要的值。
但是,您應該long優先使用s,而不是ints,因為這將顯著減少 64 位 CPU 完成的工作,而與 32 位 CPU 沒有太大區別。
對于compareTowith long[]s in little-endian:
public static int keyCompareTo(final long[] a, final long[] b) {
final int highComp = Long.compareUnsigned(a[1], b[1]);
if (highComp != 0) return highComp;
else return Long.compareUnsigned(a[0], b[0]);
}
或者,使用一個對象:
public class Key implements Comparable<Key> {
final protected long high;
final protected long low;
public int compareTo(final Key other) {
if (other == null) throw new NullPointerException();
final int highComp = Long.compareUnsigned(a.high, b.high);
if (highComp != 0) return highComp;
else return Long.compareUnsigned(a.low, b.low);
}
}
對于平等:
a[0] == b[0] && a[1] == b[1]
a.high == b.high && a.low == b.low
對于小于:
final int highComp = Long.compareUnsigned(a[1], b[1]);
final boolean lessThan = highComp < 0 || (highComp == 0 && Long.compareUnsigned(a[0], b[0]) < 0);
final int highComp = Long.compareUnsigned(a.high, b.high);
final boolean lessThan = highComp < 0 || (highComp == 0 && Long.compareUnsigned(a.low, b.low) < 0);
添加回答
舉報