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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM