[英]How to parse hex color String to Integer
I'm working on some code in Robolectric, namely IntegerResourceLoader
. 我正在研究Robolectric中的一些代码,即IntegerResourceLoader
。 The following method is throwing a RuntimeException
when rawValue
is something such as 0xFFFF0000 : 下面的方法是抛出一个RuntimeException
时候rawValue
是0xFFFF0000地址一些诸如:
@Override
public Object convertRawValue( String rawValue ) {
try {
return Integer.parseInt( rawValue );
} catch ( NumberFormatException nfe ) {
throw new RuntimeException( rawValue + " is not an integer." );
}
}
I tried using Integer.decode(String) but that throws a NumberFormatException even though the grammar appears to be correct. 我尝试使用Integer.decode(String),但即使语法看起来是正确的,也会引发NumberFormatException。
decode()
is the right method to call but it fails because 0xFFFF0000 is higher than 0x7fffffff max limit for integer. decode()
是正确的调用方法,但它失败,因为0xFFFF0000高于0x7fffffff整数的最大限制。 You may want to consider Long. 你可能想要考虑Long。
The following method is throwing a
RuntimeException
whenrawValue
is something such as0xFFFF0000
下面的方法是抛出一个RuntimeException
时候rawValue
是一些诸如0xFFFF0000
This is because Integer.parseInt
isn't designed to handle the 0x
prefix. 这是因为Integer.parseInt
不是为处理0x
前缀而设计的。
I tried using Integer.decode(String) but that throws a NumberFormatException even though the grammar appears to be correct. 我尝试使用Integer.decode(String),但即使语法看起来是正确的,也会引发NumberFormatException。
From Integer.decode
Javadoc (linked in your question): 来自Integer.decode
Javadoc(在您的问题中链接):
This sequence of characters must represent a positive value or a NumberFormatException will be thrown. 此字符序列必须表示正值或将抛出NumberFormatException。
0xFFFF0000
is a negative number, and so this is likely what's causing the exception to be thrown here. 0xFFFF0000
是一个负数,所以这可能是导致异常被抛出的原因。
Solution: 解:
If you know that the value given will be in the form 0x[hexdigits]
, then you can use Integer.parseInt(String, int)
which takes the radix. 如果你知道给定的值将是0x[hexdigits]
的形式,那么你可以使用取得基数的Integer.parseInt(String, int)
。 For hexadecimal, the radix is 16. Like so: 对于十六进制,基数是16.像这样:
return Integer.parseInt(rawValue.split("[x|X]")[1], 16);
This uses the regex [x|X]
to split the string, which will separate rawValue
on either the lower-case or upper-case "x" character, then passes it to parseInt
with a radix of 16 to parse it in hexadecimal. 这使用正则表达式[x|X]
来分割字符串,这将在小写或大写“x”字符上分隔rawValue
,然后将其传递给基数为16的parseInt
以十六进制解析它。
This is how android does it: 这是android如何做到的:
private int parseColor(String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color");
}
return (int)color;
}
throw new IllegalArgumentException("Unknown color");
}
If you can strip off the 0x from the front then you can set the radix of parseInt(). 如果你可以从前面剥离0x,那么你可以设置parseInt()的基数。 So Integer.parseInt(myHexValue,16)
所以Integer.parseInt(myHexValue,16)
See http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String , int) for more information 有关更多信息,请参阅http://docs.oracle.com/javase/6/docs/api/java/lang/Integer.html#parseInt(java.lang.String,int )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.