在Java中比较两个十六进制字符串 [英] Compare two hex strings in Java?

查看:648
本文介绍了在Java中比较两个十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Java中使用Chord协议实现一个简单的DHT。细节不重要,但我坚持的是我需要散列字符串,然后看看一个哈希字符串是小于另一个。



我有一些使用SHA1计算散列的代码,它返回一个长度为40位的十六进制字符串(在Java中为String类型),例如:

  69342c5c39e5ae5f0077aecc32c0f81811fb8193 

然而,我需要能够比较其中的两个,例如:

  0000000000000000000000000000000000000000 



<

  FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF 

这是完整的值范围,因为40位数字符串实际上代表范围内的40个十六进制数字0123456789ABCDEF

有谁知道如何做到这一点?



在此先感谢。 0..9 A..F 在ASCII字符集中以十六进制顺序排列,所以

  string1.compareTo(string2)

应该这样做。除非我错过了什么。


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.

这篇关于在Java中比较两个十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆