stringstream 和法语语言环境 (French_France.1252)
std::stringstream stream_french;
stream_french.imbue(std::locale("")); // French_France.1252
stream_french << 1000;
std::string value_french = stream_french.str();
这段代码会将 1000 转换为字符串“1 000”,但 value_french[1] 的值是 -96 而不是 32,为什么呢?
value_french[0] = 49
value_french[1] = -96
value_french[2] = 48
value_french[3] = 48
value_french[3] = 48
如果我这样做
stream_french << "1 000";
value_french[1] 的值为 32。该错误似乎与 char 的符号有关,但为什么在进行转换时它只影响空格?
std::stringstream stream_french;
stream_french.imbue(std::locale("")); // French_France.1252
stream_french << 1000;
std::string value_french = stream_french.str();
This code will convert 1000 to string "1 000" but the value of value_french[1] is -96 and not 32, why is that ?
value_french[0] = 49
value_french[1] = -96
value_french[2] = 48
value_french[3] = 48
value_french[3] = 48
If I do
stream_french << "1 000";
The value of value_french[1] is 32. The error seems to be related to the signedess of char, but why is it only affecting white spaces when doing conversions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
-96 是 160 的有符号等价物,即 0xA0;如果您去检查 Windows 1252 代码页表,你会看到这样的角色是
,即 不允许自动换行的空格:
与“100 km”一样,“1 000”也一样,很明显,在 1 和三个 0 之间不希望有换行符,因此使用不间断空格;确实很聪明。
要确保这一点:有一个“正常”空间:
1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 00 0 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 00 0 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000
带不间断空格:
1 000 1 000 1 000 1 000 1 ;000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 00 0 1 000 1 000 1 000 1 000 1 ;000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 0 00 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 00 0 1 000 1 000 1 000 1 000 1 ;000 1 000 1 000 1 000 1 000 1 000 1 000
(如果没有看到任何差异,请尝试使用浏览器的字体大小进行放大/缩小)
That -96 is the signed equivalent of 160, i.e. 0xA0; if you go and check the Windows 1252 codepage table, you'll see that such character is
which is a space that don't allow an automatic line break:
As with "100 km", also with "1 000" it's clear that it's not desirable to have a line break between the 1 and the three 0, so a non-breaking space is used; quite clever indeed.
To make it definitely clear: with a "normal" space:
1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000
with a non-breaking space:
1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000 1 000
(if you don't see any difference, try to zoom in/out with the font size of the browser)