1、parseInt()
String[] str = {"12","23","45"};
int sum = 0;
for(int i=0;i<str.length;i++){
int myint = Integer.parseInt(str[i]);
sum = sum+myint;
}
System.out.print("数组各元素之和是"+sum);
public static int parseInt(String s) throws NumberFormatException
'-'
(
'\u002D'
) to indicate a negative value or an ASCII plus sign
'+'
(
'\u002B'
) to indicate a positive value. The resulting integer value is returned, exactly as if the argument and the radix 10 were given as arguments to the
parseInt(java.lang.String, int)
method.
-
Parameters:
-
s
- aString
containing theint
representation to be parsed
Returns:
- the integer value represented by the argument in decimal. Throws:
-
NumberFormatException
- if the string does not contain a parsable integer.
2、
String str1 = Integer.toString(255);
String str2 = Integer.toBinaryString(255);
String str3 = Integer.toHexString(255);
String str4 = Integer.toOctalString(255);
System.out.print("256十进制、二进制、十六进制、八进制分别表示为:"+str1+"、"+str2+"、"+str3+"、"+str4);
运行结果:
256十进制、二进制、十六进制、八进制分别表示为:255、11111111、ff、377
toString
public static String toString(int i, int radix)
If the radix is smaller than Character.MIN_RADIX
or larger than Character.MAX_RADIX
, then the radix 10
is used instead.
If the first argument is negative, the first element of the result is the ASCII minus character'-'
('\u002D'
). If the first argument is not negative, no sign character appears in the result.
The remaining characters of the result represent the magnitude of the first argument. If the magnitude is zero, it is represented by a single zero character'0'
('\u0030'
); otherwise, the first character of the representation of the magnitude will not be the zero character. The following ASCII characters are used as digits:
0123456789abcdefghijklmnopqrstuvwxyz
These are
'\u0030'
through
'\u0039'
and
'\u0061'
through
'\u007A'
. If
radix
is
N, then the first
N of these characters are used as radix-
N digits in the order shown. Thus, the digits for hexadecimal (radix 16) are
0123456789abcdef
. If uppercase letters are desired, the
String.toUpperCase()
method may be called on the result:
Integer.toString(n, 16).toUpperCase()
-
Parameters:
-
i
- an integer to be converted to a string. -
radix
- the radix to use in the string representation.
Returns:
- a string representation of the argument in the specified radix. See Also:
-
Character.MAX_RADIX
,Character.MIN_RADIX
-
toHexString
public static String toHexString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 16.The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in hexadecimal (base 16) with no extra leading
0
s. If the unsigned magnitude is zero, it is represented by a single zero character'0'
('\u0030'
); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as hexadecimal digits:0123456789abcdef
'\u0030'
through'\u0039'
and'\u0061'
through'\u0066'
. If uppercase letters are desired, theString.toUpperCase()
method may be called on the result:Integer.toHexString(n).toUpperCase()
-
Parameters:
-
i
- an integer to be converted to a string.
Returns:
- the string representation of the unsigned integer value represented by the argument in hexadecimal (base 16). Since:
- JDK1.0.2
-
-
toOctalString
public static String toOctalString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 8.The unsigned integer value is the argument plus 232 if the argument is negative; otherwise, it is equal to the argument. This value is converted to a string of ASCII digits in octal (base 8) with no extra leading
0
s.If the unsigned magnitude is zero, it is represented by a single zero character
'0'
('\u0030'
); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The following characters are used as octal digits:01234567
'\u0030'
through'\u0037'
.-
Parameters:
-
i
- an integer to be converted to a string.
Returns:
- the string representation of the unsigned integer value represented by the argument in octal (base 8). Since:
- JDK1.0.2
-
-
toBinaryString
public static String toBinaryString(int i)
Returns a string representation of the integer argument as an unsigned integer in base 2.The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading
0
s. If the unsigned magnitude is zero, it is represented by a single zero character'0'
('\u0030'
); otherwise, the first character of the representation of the unsigned magnitude will not be the zero character. The characters'0'
('\u0030'
) and'1'
('\u0031'
) are used as binary digits.-
Parameters:
-
i
- an integer to be converted to a string.
Returns:
- the string representation of the unsigned integer value represented by the argument in binary (base 2). Since:
- JDK1.0.2
-
-
toString
public static String toString(int i)
Returns aString
object representing the specified integer. The argument is converted to signed decimal representation and returned as a string, exactly as if the argument and radix 10 were given as arguments to thetoString(int, int)
method.-
Parameters:
-
i
- an integer to be converted.
Returns:
- a string representation of the argument in base 10.
-