将一个整数转换为16进制输出(不得使用系统函数)

// 代码如下
#include<bits/stdc++.h>
using namespace std;
int main(){
    
    
	int m,temp,n=0; //n用作下标,temp为整数每次除以的16的余数 
	string a[100]; //用来存放转换后的数 
	cin>>m; 
	while(m>0){
    
    
		n++;
		temp=m%16;
		if(temp>9){
    
    
			a[n]=temp-10+'A';//将字母存入数组a 
		}else{
    
    
			a[n]='0'+temp;//将余数数字存入数组a 
		} 
		m=m/16;		
	}
	for(int i=n;i>0;i--){
    
    
		cout<<a[i];
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45053508/article/details/113095963