- package test1;
- import java.util.Arrays;
- import java.util.Scanner;
- public class Test2 {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.print("请输入一个数:");
- int num = sc.nextInt();
- getHet(num);
- }
- public static void getHet(int num) {
- getChar(num, 1, 1);
- }
- public static void getChar(int num, int base, int offset) {
- if (num == 0)
- return;
- char[] chs = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
- 'B', 'C', 'D', 'E', 'F' };
- char[] arr = new char[32];
- int l = arr.length;
- int count = 0;// 记录向数组中存放了几个字符
- while (num != 0) {
- int temp = num & base;
- arr[--l] = chs[temp];
- num = num >>> offset;
- count++;
- }
- // 对数组进行截取
- arr = Arrays.copyOfRange(arr, arr.length - count, arr.length);
- for (char c : arr) {
- System.out.print(c);
- }
- }
- }
复制代码 |