[英]C++ How to convert an unsigned integer to a hex string without a thousands separator
To store the color of a button's text, I have the variable uint32_t textColor
set to the value 0xF5CE52 (0d16109138).为了存储按钮文本的颜色,我将变量uint32_t textColor
设置为值 0xF5CE52 (0d16109138)。 Now I need to insert this value of textColor into a string as a hexadecimal value.现在我需要将此 textColor 值作为十六进制值插入到字符串中。 However, when I try to convert textColor to a hex string via a stringstream, it seems like a thousands separator is inserted in the hex string and I can't figure out how to prevent this from happening...但是,当我尝试通过字符串流将 textColor 转换为十六进制字符串时,似乎在十六进制字符串中插入了千位分隔符,我无法弄清楚如何防止这种情况发生......
To convert textColor to a hexadecimal string, I'm using the following piece of code:要将 textColor 转换为十六进制字符串,我使用了以下代码:
uint32_t textColor = 0xF5CE52;
stringstream btnColorHex;
btnColorHex << uppercase << setfill('0') << setw(6) << hex << textColor;
I print it using the following:我使用以下方法打印它:
cout << "btnColorHex.str() - " << btnColorHex.str() << endl;
Which results in the following output:结果如下 output:
btnColorHex.str() - F5C.E52
Note: I have set my system locales to format numbers in Dutch formatting, which uses.注意:我已将我的系统区域设置为使用荷兰语格式设置数字格式。 as the thousands separator and, as the decimal separator.作为千位分隔符,作为小数点分隔符。
I expected the following to be printed to the console:我希望将以下内容打印到控制台:
btnColorHex.str() - F5CE52
I had 'solved' this issue by removing the.我通过删除“解决”了这个问题。 from the string afterwards, but of course this didn't work when I tested this code on a PC with the locales set to American formatting using, as the thousands separator.之后从字符串中提取,但是当我在 PC 上测试此代码时,将语言环境设置为美国格式,使用千位分隔符,当然这不起作用。
I have also tried casting the uint32_t
to an unsgined int
, but that resulted in the same issues.我也试过将uint32_t
转换为unsgined int
,但这导致了同样的问题。
I could try using functions like sprintf()
from plain C to circumvent this issue, but I'd like to now if there is a proper C++ way to do this first.我可以尝试使用普通 C 中的sprintf()
之类的函数来规避此问题,但我想现在是否有适当的 C++ 方法首先执行此操作。
So my question is: Is there a way in C++ to format an unsigned integer as a hex string, without it inserting a thousands separator?所以我的问题是:C++ 中有没有办法将无符号的 integer 格式化为十六进制字符串,而不插入千位分隔符?
To disable the locale formating use imbue(std::locale("C"))
要禁用区域设置格式,请使用imbue(std::locale("C"))
Your code will now look like this:您的代码现在将如下所示:
uint32_t textColor = 0xF5CE52;
stringstream btnColorHex;
btnColorHex.imbue(std::locale("C"));
btnColorHex << uppercase << setfill('0') << setw(6) << hex << textColor;
Now regardless of the PC's locale the output will always be formatted correctly:现在,无论 PC 的区域设置如何,output 都将始终正确格式化:
btnColorHex.str() - F5CE52
You could just handwrite this as你可以手写这个作为
#include <cstdint>
#include <cstdlib>
#include <iostream>
std::string hex( uint32_t value ) {
char str[16];
char* p = &str[16];
do {
p--;
uint32_t digit = value % 16;
value /= 16;
*p = digit>=10 ? 'a' + (digit-10) : '0' + digit;
} while ( value > 0 );
p--;
*p = 'x';
p--;
*p = '0';
return std::string(p,&str[16]-p);
}
Running as运行为
int main()
{
for ( int j=0; j<10; ++j ) {
uint32_t value = std::rand();
std::cout << std::hex << value << " " << hex( value ) << std::endl;
}
}
Produces产品
6b8b4567 0x6b8b4567
327b23c6 0x327b23c6
643c9869 0x643c9869
66334873 0x66334873
74b0dc51 0x74b0dc51
19495cff 0x19495cff
2ae8944a 0x2ae8944a
625558ec 0x625558ec
238e1f29 0x238e1f29
46e87ccd 0x46e87ccd
Godbolt: https://godbolt.org/z/xxEnqs7es Godbolt: https://godbolt.org/z/xxEnqs7es
Another option is to use C++20's std::format
.另一种选择是使用 C++20 的std::format
。 However at the moment only gcc/clang on trunk (unreleased) have this feature implemented.然而目前只有 gcc/clang on trunk (unreleased) 实现了这个特性。
#include <format>
std::string hex( uint32_t value ) {
return std::format("0x{:x}", value);
}
Godbolt: https://godbolt.org/z/roMc5ajcj Godbolt: https://godbolt.org/z/roMc5ajcj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.