使用无符号右移-&-栈 实现 十进制转十六进制

package com.qf.day03;

import java.util.Scanner;
import java.util.Stack;

public class Dec2HexDemo {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StringBuffer sb = new StringBuffer();
		Stack stack = new Stack();
		while (true) {
			int num = scan.nextInt();
			for (int i = 0; i < 4; i++) {
				int result = num & 0xf;
				if (result > 9) {
					stack.push("" + (char) ((result - 10) + 'A'));
				} else {
					stack.push("" + result);
				}
				num = num >>> 4;
			}
			while (!stack.isEmpty()) {
				sb.append(stack.pop());
			}
			System.out.println(sb.toString());
			sb = new StringBuffer();
		}
	}
} 

你可能感兴趣的:(Java,SE)