如何在 c++ 中将十六进制整数转换为字符串(适用于 html 颜色)
How to Convert hexadecimal integer into a string in c++ (for html colors)?
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";
工程!
我没有打印好的变量。
我不删除这个问题,因为也许它可以帮助某人。
相关文章:
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- C++17 - 使用自定义分配器的节点提取/重新插入 - 适用于 clang++/libc++,但不适用于 libstd
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- 组合字符串不适用于 libCurl,C++
- 适用于 WebView2 旧版本的示例应用程序
- 重载 std::字符串运算符+ 用于打印枚举名称
- 在 NVIDIA GEFORCE GTX 1050 上下载适用于 Windows 10 的 openCL 1.2
- __attribute__(优化(0))) 是否适用于"recursively"?
- 为什么 std::erase(std::erase_if) 不是适用于<algorithm>任何容器的模板?
- 使用一个参数的模板函数时出错(适用于 2)
- 使用 适用于 Android 和 iOS 的 tf-lite C++ API
- 为什么这适用于 G++ 而不是 CLANG?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 适用于 Linux 的 c++ 上的代理脚本
- 为什么我的 SFINAE 表达式不再适用于 GCC 8.2?
- 将wchar_t字符串从/传递到本机插件-仅适用于Windows
- C++11 正则表达式是否适用于 UTF-8 字符串
- 如何在 c++ 中将十六进制整数转换为字符串(适用于 html 颜色)
- 不在异常中嵌入std::字符串的规则是否仍然适用于move构造函数
- c++字符串操作,适用于iOS和Android平台