C++ 将 RGB 转换为十六进制
C++ Convert RGB to Hex
我需要有关十六进制代码中 rgb 转换器的帮助。我正在尝试制作函数 return 十六进制代码。我需要 cString 为十六进制。对于导入,我使用:
dwTitleColor1 // Red
dwTitleColor2 // Green
dwTitleColor3 // Blue
const char * CHARACTER::GetTitleColor() const
{
static char cString[CHARACTER_NAME_MAX_LEN + 1];
dwTitleColor1 = 0
dwTitleColor2 = 0
dwTitleColor3 = 0
snprintf(cString, sizeof(cString), "r:%d, g:%d, b:%d.", dwTitleColor1, dwTitleColor2, dwTitleColor3);
return cString;
}
你为什么不使用 C++ 工具?
std::string CHARACTER::GetTitleColor() const
{
std::ostringstream buffer;
buffer.flags(std::ios_base::hex | std::ios_base::left);
buffer.fill('0');
buffer <<"r: " <<std::setw(2) <<dwTitleColor1
<<", g: " <<std::setw(2) <<dwTitleColor2
<<", b: " <<std::setw(2) <<dwTitleColor3;
return buffer.str();
}
这会将每种颜色写成一个 2 位十六进制数。随意调整格式:如果需要小数,请删除标志,如果不需要前导 0,请删除 setw
和填充。
(并重命名 class,除了 C++ 程序中的宏,您不想对任何东西使用全部大写)。
[编辑]
由于它似乎引起了一些混乱,我想声明我有意将 return 类型更改为 std::string
。因为在 C++ 中字符串是 std::string
,而不是 char*
。它的使用非常简单:
// Assuming myChar is a CHARACTER instance
std::string colorA = myChar.GetTitleColor(); // straightforward
auto colorB = myChar.GetTitleColor(); // better, color gets automatic type from method return type
const auto & colorC = myChar.GetTitleColor(); // if we won't modify it, even better.
您可以根据需要使用 returned 字符串。你不必释放它。它在超出范围之前一直有效(与您的 static char* 相反,如果您在另一个字符上调用 GetTitleColor
,它会被覆盖)。
如果你真的别无选择,你总是可以做与静态相同的事情:用这两个替换 return 行:
static std::string result = buffer.str();
return result.c_str();
它与您的静态版本具有完全相同的警告:再次调用 GetTitleColor()
将使之前 returned 的指针无效。
应该这样做:
snprintf(cString, sizeof(cString), "r:%x, g:%x, b:%x.",
dwTitleColor1, dwTitleColor2, dwTitleColor3);
我需要有关十六进制代码中 rgb 转换器的帮助。我正在尝试制作函数 return 十六进制代码。我需要 cString 为十六进制。对于导入,我使用:
dwTitleColor1 // Red
dwTitleColor2 // Green
dwTitleColor3 // Blue
const char * CHARACTER::GetTitleColor() const
{
static char cString[CHARACTER_NAME_MAX_LEN + 1];
dwTitleColor1 = 0
dwTitleColor2 = 0
dwTitleColor3 = 0
snprintf(cString, sizeof(cString), "r:%d, g:%d, b:%d.", dwTitleColor1, dwTitleColor2, dwTitleColor3);
return cString;
}
你为什么不使用 C++ 工具?
std::string CHARACTER::GetTitleColor() const
{
std::ostringstream buffer;
buffer.flags(std::ios_base::hex | std::ios_base::left);
buffer.fill('0');
buffer <<"r: " <<std::setw(2) <<dwTitleColor1
<<", g: " <<std::setw(2) <<dwTitleColor2
<<", b: " <<std::setw(2) <<dwTitleColor3;
return buffer.str();
}
这会将每种颜色写成一个 2 位十六进制数。随意调整格式:如果需要小数,请删除标志,如果不需要前导 0,请删除 setw
和填充。
(并重命名 class,除了 C++ 程序中的宏,您不想对任何东西使用全部大写)。
[编辑]
由于它似乎引起了一些混乱,我想声明我有意将 return 类型更改为 std::string
。因为在 C++ 中字符串是 std::string
,而不是 char*
。它的使用非常简单:
// Assuming myChar is a CHARACTER instance
std::string colorA = myChar.GetTitleColor(); // straightforward
auto colorB = myChar.GetTitleColor(); // better, color gets automatic type from method return type
const auto & colorC = myChar.GetTitleColor(); // if we won't modify it, even better.
您可以根据需要使用 returned 字符串。你不必释放它。它在超出范围之前一直有效(与您的 static char* 相反,如果您在另一个字符上调用 GetTitleColor
,它会被覆盖)。
如果你真的别无选择,你总是可以做与静态相同的事情:用这两个替换 return 行:
static std::string result = buffer.str();
return result.c_str();
它与您的静态版本具有完全相同的警告:再次调用 GetTitleColor()
将使之前 returned 的指针无效。
应该这样做:
snprintf(cString, sizeof(cString), "r:%x, g:%x, b:%x.",
dwTitleColor1, dwTitleColor2, dwTitleColor3);