所以,在 Java 中,您知道如何像这样声明整数:
So, In Java, you know how you can declare integers like this:
int hex = 0x00ff00;
我认为您应该能够逆转该过程.我有这个代码:
I thought that you should be able to reverse that process. I have this code:
Integer.valueOf(primary.getFullHex());
其中 primary 是自定义 Color 类的对象.它的构造函数接受一个整数表示不透明度(0-99)和一个十六进制字符串(例如 00ff00).
where primary is an object of a custom Color class. It's constructor takes an Integer for opacity (0-99) and a hex String (e.g. 00ff00).
这是 getFullHex 方法:
public String getFullHex() {
return ("0x" + hex);
}
当我调用此方法时,它会给出我的 NumberFormatException:
When I call this method it gives my this NumberFormatException:
java.lang.NumberFormatException: For input string: "0xff0000"
我不明白发生了什么.谁能解释一下?
I can't understand what's going on. Can someone please explain?
这会有帮助吗?
Integer.parseInt("00ff00", 16)
16 表示您应该将字符串解释为基于 16 的(十六进制).使用 2 可以解析二进制数,8 代表八进制.10 是默认值,解析十进制数.
16 means that you should interpret the string as 16-based (hexadecimal). By using 2 you can parse binary number, 8 stands for octal. 10 is default and parses decimal numbers.
在您的情况下, Integer.parseInt(primary.getFullHex(), 16) 将不起作用,因为 0x 前缀由 getFullHex()代码> - 摆脱,你会没事的.
In your case Integer.parseInt(primary.getFullHex(), 16) won't work due to 0x prefix prepended by getFullHex() - get rid of and you'll be fine.
这篇关于将十六进制字符串解析为整数会引发 NumberFormatException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)