问题描述
我正在使用 Java 中的 Chord 协议实现一个简单的 DHT.细节并不重要,但我坚持的事情是我需要散列字符串,然后查看一个散列字符串是否小于"另一个.
I am implementing a simple DHT using the Chord protocol in Java. The details are not important but the thing I'm stuck on is I need to hash strings and then see if one hashed string is "less than" another.
我有一些代码可以使用 SHA1 计算哈希,它返回一个 40 位长的十六进制字符串(Java 中的 String 类型),例如:
I have some code to compute hashes using SHA1 which returns a 40 digit long hex string (of type String in Java) such as:
69342c5c39e5ae5f0077aecc32c0f81811fb8193
但是我需要能够比较其中的两个,例如:
However I need to be able to compare two of these so to tell, for example that:
0000000000000000000000000000000000000000
小于:
FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
这是完整的值范围,因为 40 位字符串实际上代表 0123456789ABCDEF 范围内的 40 个十六进制数字
This is the complete range of values as the 40 digit string is actually representing 40 hex numbers in the range 0123456789ABCDEF
有人知道怎么做吗?
提前致谢.
推荐答案
0..9
和 A..F
的值在ASCII 字符集,所以
The values 0..9
and A..F
are in hex-digit order in the ASCII character set, so
string1.compareTo(string2)
应该可以解决问题.除非我遗漏了什么.
should do the trick. Unless I'm missing something.
这篇关于比较 Java 中的两个十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!