C++ | Int转十六进制字符串

//i要转化的十进制整数,width转化后的宽度,位数不足则补0 
std::string dec2hex(int i, int width)
 {
    std::stringstream ioss; //定义字符串流 
    std::string s_temp; //存放转化后字符
    ioss << std::hex << i; //以十六制形式输出 
    ioss >> s_temp; 
    if (width > s_temp.size()) { 
        std::string s_0(width - s_temp.size(), '0'); //位数不够则补0                 
        s_temp = s_0 + s_temp; //合并
    } 
    std::string s = s_temp.substr(s_temp.length() - width,   s_temp.length()); //取右width位 
    return s;
 }    

转载自:https://my.oschina.net/u/3273849/blog/3102150

posted @ 2021-12-21 10:43  zhxzh  阅读(2517)  评论(0编辑  收藏  举报