将十六进制字符串转换为十进制整数 [英] Converting Hexadecimal String to Decimal Integer
问题描述
我写了一些代码将我的十六进制显示字符串转换为十进制整数。然而,当输入是像100a或625b(有字母的东西)时,我得到了这样的错误:
java.lang.NumberFormatException:对于输入字符串:$ 100
java.lang.NumberFormatException.forInputString(Unknown Source)at
java.lang.Integer.parseInt(Unknown Source)
你知道如何将字符串转换为十进制整数?
<$ c如果(display.getText()!= null)
{
if(display.getText()。contains(a)|| display.getText()。contains(b )||
display.getText()。contains(c)|| display.getText()。contains(d)||
display.getText()。contains(e )|| display.getText()。contains(f))
{
temp1 = Integer.parseInt(display.getText(),16);
temp1 =(double)temp1;
}
else
{
temp1 = Double.parseDouble(String.valueOf(display.getText()));
}
}
它看起来你的字符串中有一个额外的空格字符。您可以使用 trim()
删除前后空格:
temp1 = Integer.parseInt(display.getText()。trim(),16);
或者如果您认为存在空间意味着存在其他问题,则必须查看因为我们没有剩下的代码。
I wrote some code to convert my hexadecimal display string to decimal integer. However, when input is something like 100a or 625b( something with letter) I got an error like this:
java.lang.NumberFormatException: For input string: " 100a" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source)
Do you have any idea how can I convert my string with letters to decimal integer?
if(display.getText() != null)
{
if(display.getText().contains("a") || display.getText().contains("b") ||
display.getText().contains("c") || display.getText().contains("d") ||
display.getText().contains("e") ||display.getText().contains("f"))
{
temp1 = Integer.parseInt(display.getText(), 16);
temp1 = (double) temp1;
}
else
{
temp1 = Double.parseDouble(String.valueOf(display.getText()));
}
}
It looks like there's an extra space character in your string. You can use trim()
to remove leading and trailing whitespaces:
temp1 = Integer.parseInt(display.getText().trim(), 16 );
Or if you think the presence of a space means there's something else wrong, you'll have to look into it yourself, since we don't have the rest of your code.
这篇关于将十六进制字符串转换为十进制整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!