如何在 c++ 中将十六进制整数转换为字符串(适用于 html 颜色)

How to Convert hexadecimal integer into a string in c++ (for html colors)?

本文关键字:字符串 适用于 html 颜色 转换 整数 c++ 十六进制      更新时间:2023-10-16

Bonjourno, Bonsoir, Konbanwa, Ave [...]

我尝试将 int 转换为十六进制,然后转换为字符串以生成 html 颜色......

你知道一种方法吗?

我试过:

        int rouge=  0x0 ;    //red in french
        int vert=   0x0 ;    //green
        int bleu=   0xff;    //blue
        int couleur=0x0 ;    //color
        bleu*=256*256;
        vert*=256;
        couleur=rouge+vert+bleu; 
        cout<<"couleur"<<couleur<<endl;
        stringstream ss2;    // #include <sstream> if someone want to do it also ^^
        ss2 <<hex<<couleur; // convert  int to stringstream  works if decimal
        cout<<ss2<<hex<<endl;
        string string_couleur = ss2.str();  // convert streamstring to string
        for (int nombre_0_devant=6-string_couleur.size(); nombre_0_devant>0;nombre_0_devant--) string_couleur="0"+string_couleur;  // just a line to add needed 0   ff --> 0000ff
        html+="#"+string_couleur+">";

它给出:

0x28f738       

:(

感谢您的阅读!

抱歉

cout<<"string"<<string_couleur<<"n";

工程!

我没有打印好的变量。

我不删除这个问题,因为也许它可以帮助某人。