十进制转为十六进制

#include <stdio.h>
#include <stack>

int main() {
  std::stack<char> s;
  int num, mod;

  printf("输入十进制数: \n");
  scanf("%d", &num);
  printf("num: %d\n", num);

  while(num/16 != 0) {
    mod = num%16;
    num = num/16;

    if(mod>=0 && mod<=9) {
      s.push(
'0'+mod);     }     if(mod>=10 && mod<=15) {       s.push('A'+(mod-10));     }   }   if(num>=0 && num<=9) {     s.push('0'+num);   }   if(num>=10 && num<=15) {     s.push('A'+(num-10));   }   printf("0x");   while(!s.empty()) {     printf("%c", s.top());     s.pop();   }   printf("\n");   return 0; }

 

posted @ 2024-01-16 11:47  东宫得臣  阅读(5)  评论(0编辑  收藏  举报