如何将字符串中的hex-int解析为Integer? [英] How do I parse a hex-int from a string to an Integer?

查看:349
本文介绍了如何将字符串中的hex-int解析为Integer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写这个没问题:(它也没有给出任何错误)

I have no problem in writing this: (it also doesn't give any errors)

int hex = 0xFFFFFFFF;

整数也必须有一个alpha值!

The integer has to have an alpha value also!

当我尝试这样做时:

Integer hex = Integer.parseInt("0xFFFFFFFF");
// Or I try this:
Integer hex = Integer.parseInt("FFFFFFFF");

我得到 java.lang.NumberFormatException:对于输入字符串:0xFFFFFFFF 抛出!

为什么这不起作用?

我在猜还有一些其他方法来解析我刚才没有意识到的十六进制整数,我真的需要能够从字符串中解析十六进制为我正在制作的程序。

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)

Integer.parseInt(x)只需调用Integer.parseInt(x,10),所以你必须指定一个基数为10的数字。

Integer.parseInt(x) simply calls Integer.parseInt(x, 10) , so you have to specify a radix 10 number.

为了解析十六进制字符串,你只需要使用以下(请注意,不允许使用0x前缀):

In order to parse a hex string, you would simply have to use the following (note that the 0x prefix is not allowed):

Integer.parseInt("FFFFFFF", 16);

这篇关于如何将字符串中的hex-int解析为Integer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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