用C++实现10进制转16进制

#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
//将任意字符转换为十进制,其中a-z代表10-35,A-Z代表36-61,用对应的ASCII码调整就好
char i2char(int n)
{
    if(n>=0&&n<=9)return '0'+n;
    if(n>=10&&n<=35)return 'a'+(n-10);
    else return 'A'+(n-36);
}
template<typename T> string num2str62(T& n)
{
    char buf[1024]={0};
    int index=0;
    while(n>0)
    {
        int m=n%62;
        n-=m;
        n/=62;
        buf[index++]=i2char(m);
    }
    string str=buf;
    string str2=string(str.rbegin(),str.rend());
    return str2;
}
int main(int narg,char** args)
{
    int n=10245;
    
    printf("%s\n",num2str62(n).c_str());
    return 0;

};

 

相关文章:

  • 2021-07-20
  • 2021-12-04
  • 2021-11-13
  • 2021-11-22
  • 2022-02-28
猜你喜欢
  • 2022-02-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2022-12-23
  • 2021-10-24
  • 2022-12-23
相关资源
相似解决方案