在Java中解析整数字符串 [英] Parsing integer strings in Java

查看:377
本文介绍了在Java中解析整数字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在java中将十六进制字符串转换为long?

我知道Java无法解决这个问题:

I know that Java can't handle this:

Integer.parseInt("0x64")

相反,你必须这样做:

Integer.parseInt("64", 16)

是否有内置于Java中的内容可以使用十六进制,八进制的标准前缀和十进制前缀缺少来自动为我解析整数字符串(这样我就不必去除前缀并明确设置基数) ?

Is there something built into Java that can automatically parse integer strings for me using the standard prefixes for hex, octal, and lack of prefix for decimal (so that I don't have to strip off the prefix and explicitly set the base)?

推荐答案

您可以使用 Integer.decode

You can use Integer.decode:

Integer.decode("0x64");

解码规则如下:

Prefix  Type
        DecimalNumeral
0x      HexDigits
0X      HexDigits
#       HexDigits
0       OctalDigits

注意:<$ href =http:// download中提供 decode 。 oracle.com/javase/6/docs/api/java/lang/Short.html#decode%28java.lang.String%29rel =noreferrer> 整数

Note: decode is available in Short, Integer, and Long.

这篇关于在Java中解析整数字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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