开发者

How to convert binary, OCT, HEX to calculate in Java?

开发者 https://www.devze.com 2022-12-26 16:19 出处:网络
Write an application that inputs one number consisting of FIVE digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces ea

Write an application that inputs one number consisting of FIVE digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 12345, the program should print

1   2   3   4   5

The following screen dump of result is for your reference.

Input a digit: 12345
Digits in 12345 开发者_StackOverflow= 1 2 3 4 5

How to convert binary, OCT, HEX to calculate the question?

int digit1, digit2, digit3, digit4, digit5;
digit1 = number / 10000;
digit2 = number % 10000 / 1000;
digit3 = number % 1000 / 100;
digit4 = number % 100 / 10;
digit5 = number % 10;


You can make use of the methods the java.lang.Integer class provides, such as toBinaryString(), toOctalString() and toHexString() and the toString() and valueOf() methods taking a radix (which is the base, e.g. binary is 2, octal is 8, etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消