发表于: java/j2ee | 作者: | 日期: 2011/7/07 02:07

简单的说,二进制就是逢二进一,八进制就是逢八进一,十进制就是逢十进一,十六进制就是逢十六进一。关于进制更多的基础知识可以参考以下链接:http://www.d2school.com/bcyl/bhcpp/newls/ls06.htm。java提供了十进制与二进制、八进制、十六进制间相互转换的方法。

十进制转成二进制
Integer.toBinaryString(int i)

十进制转成八进制
Integer.toOctalString(int i)

十进制转成十六进制:
Integer.toHexString(int i)

示例:

int programId = 20;
System.out.println(Integer.toBinaryString(programId));
System.out.println(Integer.toOctalString(programId));
System.out.println(Integer.toHexString(programId));

输出结果:

10100
24
14

二进制转十进制
Integer.valueOf(“0101”,2).toString()

八进制转成十进制
Integer.valueOf(“876”,8).toString()

十六进制转成十进制
Integer.valueOf(“FFFF”,16).toString()


String str1 = “10100”;
String str2 = “24”;
String str3 = “14”;
System.out.println(Integer.parseInt(str1, 2));
System.out.println(Integer.parseInt(str2, 8));
System.out.println(Integer.parseInt(str3, 16));

输出结果:

20
20
20

方法说明:

int java.lang.Integer.parseInt(String s, int radix) throws NumberFormatException

Parses the string argument as a signed integer in the radix specified by the second argument. The characters in the string must all be digits of the specified radix (as determined by whether java.lang.Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign ‘-‘ (‘\u002D’) to indicate a negative value. The resulting integer value is returned.

An exception of type NumberFormatException is thrown if any of the following situations occurs:

The first argument is null or is a string of length zero.

The radix is either smaller than java.lang.Character.MIN_RADIX or larger than java.lang.Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign ‘-‘ (‘\u002D’) provided that the string is longer than length 1.
The value represented by the string is not a value of type int.

Examples:

parseInt(“0”, 10) returns 0
parseInt(“473”, 10) returns 473
parseInt(“-0”, 10) returns 0
parseInt(“-FF”, 16) returns -255
parseInt(“1100110”, 2) returns 102
parseInt(“2147483647”, 10) returns 2147483647
parseInt(“-2147483648”, 10) returns -2147483648
parseInt(“2147483648”, 10) throws a NumberFormatException
parseInt(“99”, 8) throws a NumberFormatException
parseInt(“Kona”, 10) throws a NumberFormatException
parseInt(“Kona”, 27) returns 411787

Parameters:
s the String containing the integer representation to be parsed

radix the radix to be used while parsing s.

Returns:
the integer represented by the string argument in the specified radix.

Throws:
NumberFormatException – if the String does not contain a parsable int.

功能相同,但是返回结果为Integer类型:

Integer java.lang.Integer.valueOf(String s, int radix) throws NumberFormatException

Returns an Integer object holding the value extracted from the specified String when parsed with the radix given by the second argument. The first argument is interpreted as representing a signed integer in the radix specified by the second argument, exactly as if the arguments were given to the parseInt(java.lang.String, int) method. The result is an Integer object that represents the integer value specified by the string.

In other words, this method returns an Integer object equal to the value of:

new Integer(Integer.parseInt(s, radix))

Parameters:
s the string to be parsed.

radix the radix to be used in interpreting s

Returns:
an Integer object holding the value represented by the string argument in the specified radix.

Throws:
NumberFormatException – if the String does not contain a parsable int.

: https://blog.darkmi.com/2011/07/07/1938.html

本文相关评论 - 1条评论都没有呢
Post a comment now » 本文目前不可评论

No comments yet.

Sorry, the comment form is closed at this time.