[英]How do I parse a hex-int from a string to an Integer?
I have no problem in writing this: (it also doesn't give any errors) 我写这个没有问题:(它也没有给出任何错误)
int hex = 0xFFFFFFFF;
The integer has to have an alpha value also! 该整数还必须具有一个alpha值!
But when I try to do this: 但是当我尝试这样做时:
Integer hex = Integer.parseInt("0xFFFFFFFF");
// Or I try this:
Integer hex = Integer.parseInt("FFFFFFFF");
I get a java.lang.NumberFormatException: For input string: "0xFFFFFFFF"
thrown! 我得到java.lang.NumberFormatException: For input string: "0xFFFFFFFF"
抛出java.lang.NumberFormatException: For input string: "0xFFFFFFFF"
!
Why is this not working? 为什么这不起作用?
I'm guessing that there is some other way to parse hex integers that I am just not realizing, and I really need to be able to parse hex from string for a program I am making. 我猜想还有其他一些我尚未意识到的解析十六进制整数的方法,而且我确实需要能够从字符串解析十六进制以生成正在编写的程序。
There is actually a separate function where you can define the radix: 实际上,有一个单独的函数可以定义基数:
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int) https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)
Integer.parseInt(x)
simply calls Integer.parseInt(x, 10)
, so you have to specify a radix 10 number. Integer.parseInt(x)
仅调用Integer.parseInt(x, 10)
,因此您必须指定一个基数为10的数字。
In order to parse a hex string, you would simply have to use the following (note that the 0x prefix is not allowed): 为了解析十六进制字符串,您只需要使用以下内容(请注意,不允许使用0x前缀):
Integer.parseInt("FFFFFFF", 16);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.