Java base for the conversion (Blue Bridge Cup questions summary)

Blue Bridge Cup a few exercises are related to binary conversion, write a little blog here about the record.

Here the data does not exceed the range of int , for example, the methods used are all Integer class.

Other binary decimal turn

With the Integer.parseInt (a, b) method, a binary representation of another string, b is the radix, thus converted to a decimal out.

int a = Integer.parseInt(StringOfNumber, radix);

Decimal turn the other band

With Integer.toString (i, radix) method, i denotes a decimal number, radix showing another will be converted to binary.

String s = Integer.toString(i, radix);

There are some special methods, the corresponding特殊的进制

//对应十进制转二进制
Integer.toBinaryString(i);//
//对应十进制转八进制
Integer.toOctalString(i);//
//对应十进制转十六进制
Integer.toHexString(i);//

Other band turn the other band

Through intermediate conversion to decimal.

Note: In the title should pay attention to the range of data, it is possible to convert a decimal is too large, this time to use a larger scale, such as Long
Here Insert Picture Description

Published 41 original articles · won praise 94 · views 9553

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104932071