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

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

问题描述

我正在使用 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..9A..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 中的两个十六进制字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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