如何将十六进制颜色字符串解析为整数 [英] How to parse hex color String to Integer

查看:233
本文介绍了如何将十六进制颜色字符串解析为整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Robolectric中编写一些代码,即 IntegerResourceLoader 。当 rawValue 类似于 0xFFFF0000 时,以下方法抛出 RuntimeException



  @Override 
public Object convertRawValue(String rawValue){
try {
return Integer.parseInt (rawValue);
} catch(NumberFormatException nfe){
throw new RuntimeException(rawValue +不是整数。);


我试过使用 Integer.decode(String)但是即使语法看起来正确,也会抛出NumberFormatException异常。

解决方案

decode()是调用的正确方法,但因为0xFFFF0000高于整数的0x7fffffff最大限制而失败。您可能需要考虑Long。

I'm working on some code in Robolectric, namely IntegerResourceLoader. The following method is throwing a RuntimeException when rawValue is something such as 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.

解决方案

decode() is the right method to call but it fails because 0xFFFF0000 is higher than 0x7fffffff max limit for integer. You may want to consider Long.

这篇关于如何将十六进制颜色字符串解析为整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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