c++ - std::tolower 的行为在不同的语言环境中如何变化?

标签 c++ locale tolower

我正在阅读 documentation for std::tolower at cppreference.com :

Converts the given character to lowercase according to the character conversion rules defined by the currently installed C locale.

In the default "C" locale, the following uppercase letters ABCDEFGHIJKLMNOPQRSTUVWXYZ are replaced with respective lowercase letters abcdefghijklmnopqrstuvwxyz.

这种行为在不同的语言环境中会发生怎样的变化?

最佳答案

实际上,网站上的示例显示了不同之处:

#include <iostream>
#include <cctype>
#include <clocale>

int main()
{
    unsigned char c = '\xb4'; // the character Ž in ISO-8859-15
                              // but ´ (acute accent) in ISO-8859-1 

    std::setlocale(LC_ALL, "en_US.iso88591");
    std::cout << std::hex << std::showbase;
    std::cout << "in iso8859-1, tolower('0xb4') gives "
              << std::tolower(c) << '\n';
    std::setlocale(LC_ALL, "en_US.iso885915");
    std::cout << "in iso8859-15, tolower('0xb4') gives "
              << std::tolower(c) << '\n';
}

输出:

in iso8859-1, tolower('0xb4') gives 0xb4
in iso8859-15, tolower('0xb4') gives 0xb8

因为 C 语言没有编码的概念,char(因此 char const*)只是字节。切换语言环境时,您会切换这些字节的解释,例如此处字节 0xb4 (180) 超出 ASCII 范围 (0-127),因此其含义根据您切换到的语言环境而变化:

  • 在ISO-8859-1中,表示´,因此从上往下移动时不变
  • 在 ISO-8859-15 中,它表示 Ž,因此在从上到下移动时变为 ž(在该语言环境中为 0xb8)

你会认为在后 Unicode 世界中,这将是无关紧要的,但许多人还没有过渡到 Unicode...

关于c++ - std::tolower 的行为在不同的语言环境中如何变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25236733/

相关文章:

c++ - 如何将 std::string 的实例转换为小写

谁能详细告诉我,为什么我要看这个程序的警告?

c++ - 为什么我解密后的数据格式是这样的?

c++ - std::vector::erase() 抛出的异常

c++ - 有没有影响宽字符编码的语言环境?

c++ - 如何更改默认语言环境的千位分隔符?

javascript - 对象 foo 没有方法 'toLowercase'

c++ - clang 对 C++ 11 lambda 的支持

C++ block scope extern declaration linkage,混淆C++标准解释

java - 是否可以回退到 Java 资源包的宏语言(例如 nynorsk -> norsk)