java十六进制之间比较大小,在Java中比较两个十六进制字符串

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.

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

is less than:

FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

This is the complete range of values as the 40 digit string is actually representing 40 hex numbers in the range 0123456789ABCDEF

Does anyone know how to do this?

Thanks in advance.

解决方案

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.

  • 1
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值