android 字串转时间 android字符串转int

您所在的位置:网站首页 16进制时间戳转时间 android 字串转时间 android字符串转int

android 字串转时间 android字符串转int

2023-06-25 01:24| 来源: 网络整理| 查看: 265

【问题】

需要将一个字符串,可能是十进制:

比如

123

也可能是16进制:

0x123

转换为int值。

【折腾过程】

1.参考:

去试试,void strToIntTest(){

String testStr1 = "123";

String testStr2 = "0x123";

int convertedIntValue1 = Integer.parseInt(testStr1);

int convertedIntValue2 = Integer.parseInt(testStr2);

System.out.println("convertedIntValue1=" + convertedIntValue1 + ", convertedIntValue2=" + convertedIntValue2);

}

应该就可以了。

结果调试发现:int convertedIntValue2 = Integer.parseInt(testStr2);

会挂掉。

2.然后自己去试试,通过自带文档,发现有个decode:

android 字串转时间 android字符串转int_android 字串转时间

然后去试试:void strToIntTest(){

String testStr1 = "123";

String testStr2 = "0x456";

int convertedIntValue1 = Integer.parseInt(testStr1);

//int convertedIntValue2 = Integer.parseInt(testStr2);

int convertedIntValue2 = Integer.decode(testStr2);

System.out.println("convertedIntValue1=" + convertedIntValue1 + ", convertedIntValue2=" + convertedIntValue2);

}

结果,虽然工作了。

却又遇到:

【总结】

直接调用

Integer.parseInt

即可将string转为int,但是不支持0x123的形式。

最后去用:

Integer.decode("0x123");

才可以。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3