c++ int 转成16进制类的字符串。如RGB(255,0,0)转成 FF0000字符串

c++ int 转成16进制。如RGB(255,0,0)转成 0xFF0000字符串

class XXXXXXX
{
    std::string Int2hex(int i, int width);
    QString ConvertQColorToString(QColor rgb);
}

	






#include
#include

QString XXXXXXX::ConvertQColorToString(QColor rgb)
{
    QString strItemColor = "";
    string str_r = Int2hex(rgb.red(),2);
    strItemColor+=str_r.c_str();
    string str_g = Int2hex(rgb.green(),2);
    strItemColor+=str_g.c_str();
    string str_b = Int2hex(rgb.blue(),2);
    strItemColor+=str_b.c_str();

    return strItemColor;
}


//i要转化的十进制整数,width转化后的宽度,位数不足则补0
std::string XXXXXXX::Int2hex(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;
 }

使用如下

XXXXXXX  tempClass;
 QString strColor = tempClass.ConvertQColorToString(QColor(255,255,255));

输出为"FFFFFF"

你可能感兴趣的:(qt,c++,蓝桥杯,开发语言)