This question already has an answer here:
这个问题在这里已有答案:
- Compare two hex strings in Java? 4 answers
比较Java中的两个十六进制字符串4个答案
I have two hex numbers say 'X' and 'Z'. I get a hex input from user say 'Y'. Now i need to verify whether the value 'Y' lies between 'X' and 'Z'.
All the three numbers are in Hexa decimal. is there any simple logic to achieve this other than incrementing the values of 'X' till 'Z'?
Also please suggest me how we can achieve this in JAVA.
我有两个十六进制数字说'X'和'Z'。我从用户说'Y'得到十六进制输入。现在我需要验证值'Y'是否位于'X'和'Z'之间。所有这三个数字都是十六进制的。是否有任何简单的逻辑来实现这一点,而不是将'X'的值递增到'Z'?另外请建议我如何在JAVA中实现这一目标。
3 个解决方案
#1
1
final String X = "00";
final String Z = "ff";
final String Y = "10";
if(Integer.parseInt(X, 16) <= Integer.parseInt(Y, 16) && Integer.parseInt(Y, 16) <= Integer.parseInt(Z, 16)) {
System.out.println("X <= Y <= Z");
}
#2
1
Hexadecimal values are integers - just represented in hex instead of decimal.
Can't you just do this?
十六进制值是整数 - 仅以十六进制表示而不是十进制。你不能这样做吗?
int upperHex = 0xf7c0;
int lowerHex = 0x040;
int inputHex = 0x050;
if(inputHex <= upperHex && inputHex >= lowerHex)
//Inside the range
#3
0
Numbers are just numbers, hex is a representation of that number (using a literal or String). You can: Convert the hex-Strings to Integers and then use ordinary comparison (<= < ...
) to check if the input is in between.
数字只是数字,十六进制是该数字的表示(使用文字或字符串)。您可以:将十六进制字符串转换为整数,然后使用普通比较(<= <...)来检查输入是否介于两者之间。
#1
1
final String X = "00";
final String Z = "ff";
final String Y = "10";
if(Integer.parseInt(X, 16) <= Integer.parseInt(Y, 16) && Integer.parseInt(Y, 16) <= Integer.parseInt(Z, 16)) {
System.out.println("X <= Y <= Z");
}
#2
1
Hexadecimal values are integers - just represented in hex instead of decimal.
Can't you just do this?
十六进制值是整数 - 仅以十六进制表示而不是十进制。你不能这样做吗?
int upperHex = 0xf7c0;
int lowerHex = 0x040;
int inputHex = 0x050;
if(inputHex <= upperHex && inputHex >= lowerHex)
//Inside the range
#3
0
Numbers are just numbers, hex is a representation of that number (using a literal or String). You can: Convert the hex-Strings to Integers and then use ordinary comparison (<= < ...
) to check if the input is in between.
数字只是数字,十六进制是该数字的表示(使用文字或字符串)。您可以:将十六进制字符串转换为整数,然后使用普通比较(<= <...)来检查输入是否介于两者之间。