Java中将字符转换为整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19388037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:42:08  来源:igfitidea点击:

Converting characters to integers in Java

javaintegertype-conversioncharacterprimitive

提问by JRR

Can someone please explain to me what is going on here:

有人可以向我解释这里发生了什么:

char c = '+';
int i = (int)c;
System.out.println("i: " + i + " ch: " + Character.getNumericValue(c));

This prints i: 43 ch:-1. Does that mean I have to rely on primitive conversions to convert charto int? So how can I convert a Characterto Integer?

这打印i: 43 ch:-1. 这是否意味着我必须依赖原始转换才能转换charint?那么如何将 a 转换CharacterInteger

Edit:Yes I know Character.getNumericValuereturns -1if it is not a numeric value and that makes sense to me. The question is: why does doing primitive conversions return 43?

编辑:是的,如果它不是数值,我知道Character.getNumericValue返回-1,这对我来说很有意义。问题是:为什么进行原始转换会返回43

Edit2:43is the ASCII for +, but I would expect the cast to not succeed just like getNumericValuedid not succeed. Otherwise that means there are two semantic equivalent ways to perform the same operation but with different results?

Edit2:43是 的 ASCII +,但我希望转换不会成功,就像getNumericValue没有成功一样。否则这意味着有两种语义等价的方式来执行相同的操作但结果不同?

回答by SLaks

As the documentation clearly states, Character.getNumericValue()returns the character's value as a digit.
It returns -1if the character is not a digit.

正如文档明确指出的那样,Character.getNumericValue()将字符的值作为数字返回。如果字符不是数字
,则返回-1

If you want to get the numeric Unicode code point of a boxed Characterobject, you'll need to unbox it first:

如果要获取装箱Character对象的数字 Unicode 代码点,则需要先将其拆箱:

int value = (int)c.charValue();

回答by Aurand

From the Javadoc for Character#getNumericValue:

来自 Javadoc 的Character#getNumericValue

If the character does not have a numeric value, then -1 is returned. If the character has a numeric value that cannot be represented as a nonnegative integer (for example, a fractional value), then -2 is returned.

如果字符没有数值,则返回 -1。如果字符具有不能表示为非负整数的数值(例如,小数值),则返回 -2。

The character +does not have a numeric value, so you're getting -1.

该字符+没有数值,因此您得到 -1。

Update:

更新:

The reason that primitive conversion is giving you 43 is that the the character '+' is encoded as the integer 43.

原始转换为您提供 43 的原因是字符“+”被编码为整数 43。

回答by Trying

Character.getNumericValue(c)

The java.lang.Character.getNumericValue(char ch)returns the intvalue that the specified Unicode character represents. For example, the character '\u216C'(the roman numeral fifty) will return an int with a value of 50.

java.lang.Character.getNumericValue(char ch)返回int指定的Unicode字符表示的值。例如,字符'\u216C'(罗马数字 50)将返回值为 50 的 int。

The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A')forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

大写('\u0041' through '\u005A')、小写('\u0061' through '\u007A')和全角变体('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A')形式的字母 AZ具有从 10 到 35 的数值。这与 Unicode 规范无关,Unicode 规范不为这些 char 值分配数值。

This method returns the numeric value of the character, as a nonnegative int value;

-2 if the character has a numeric value that is not a nonnegative integer;

-1 if the character has no numeric value.

此方法返回字符的数值,作为非负 int 值;

-2 如果字符的数值不是非负整数;

-1 如果字符没有数值。

And hereis the link.

这里是链接。

回答by Fareed Quraishi

43 is the dec ascii number for the "+" symbol. That explains why you get a 43 back. http://en.wikipedia.org/wiki/ASCII

43 是“+”符号的十进制 ascii 数。这就解释了为什么你会得到 43 分。 http://en.wikipedia.org/wiki/ASCII

回答by Mani P

Try any one of the below. These should work:

尝试以下任一方法。这些应该有效:

int a = Character.getNumericValue('3');
int a = Integer.parseInt(String.valueOf('3');

回答by Lahiru Rajeew Ananda

public class IntergerParser {

public static void main(String[] args){
String number = "+123123";
System.out.println(parseInt(number));
}

private static int parseInt(String number){
    char[] numChar = number.toCharArray();
    int intValue = 0;
    int decimal = 1;
    for(int index = numChar.length ; index > 0 ; index --){
        if(index == 1 ){
            if(numChar[index - 1] == '-'){
                return intValue * -1;
            } else if(numChar[index - 1] == '+'){
                return intValue;
            }
        }
        intValue = intValue + (((int)numChar[index-1] - 48) * (decimal));
        System.out.println((int)numChar[index-1] - 48+ " " + (decimal));
        decimal = decimal * 10;
    }
    return intValue;
}