Java如何将字符串解码为整数?
静态Integer.decode()方法可用于将数字的字符串表示形式转换为Integer对象。在此方法的幕后,调用Integer.valueOf(Strings,intradix)。
该字符串可以与可选的负号,随后用基数启动指定如0x,0X,#为十六进制值,0(零)为八进制数。
package org.nhooo.example.lang;
public class IntegerDecode {
public static void main(String[] args) {
String decimal = "10"; //小数
String hexa = "0XFF"; //十六进制
String octal = "077"; //八进制
Integer number = Integer.decode(decimal);
System.out.println("String [" + decimal + "] = " + number);
number = Integer.decode(hexa);
System.out.println("String [" + hexa + "] = " + number);
number = Integer.decode(octal);
System.out.println("String [" + octal + "] = " + number);
}
}上面的代码片段的结果:
String [10] = 10 String [0XFF] = 255 String [077] = 63
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短