stringstream 和法语语言环境 (French_France.1252)

发布于 2024-09-28 11:44:31 字数 541 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2024-10-05 11:44:31

-96 是 160 的有符号等价物,即 0xA0;如果您去检查 Windows 1252 代码页表,你会看到这样的角色是

A0 = U+00A0:不间断空格

,即 不允许自动换行的空格

文本处理软件通常假设可以在出现空格字符的任何位置插入自动换行符;不间断的空格可以防止这种情况发生(当然,前提是软件能够识别该字符)。例如,如果文本“100 km”不太适合行尾,则软件可能会在“100”和“km”之间插入换行符。为了避免这种不良行为,编辑者可以选择在“100”和“km”之间使用不间断空格。这保证了文本“100 km”不会被破坏:如果它不适合一行的末尾,它将整个移动到下一行。

与“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

A0 = U+00A0 : NO-BREAK SPACE

which is a space that don't allow an automatic line break:

Text-processing software typically assumes that an automatic line break may be inserted anywhere a space character occurs; a non-breaking space prevents this happening (provided the software recognises the character, of course). For example, if the text "100 km" will not quite fit at the end of a line, the software may insert a line break between "100" and "km". To avoid this undesirable behaviour, the editor may choose to use a non-breaking space between "100" and "km". This guarantees that the text "100 km" will not be broken: if it does not fit at the end of a line it is moved in its entirety to the next line.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文