如何在C++; 我正在学习C++中的Unicode,而且很难使它正常工作。我试图将单个字符视为uint64\t。如果我只需要打印出字符,它就可以工作,但问题是我需要将它们转换为大写。我可以将大写字母存储在一个数组中,并简单地使用与小写字母相同的索引,但我正在寻找一个更优雅的解决方案。我发现这很相似,但大多数答案都使用了宽字符,这是我无法使用的。以下是我的尝试: #include <iostream> #include <locale> #include <string> #include <cstdint> #include <algorithm> // hacky solution to store a multibyte character in a uint64_t #define E(c) ((((uint64_t) 0 | (uint32_t) c[0]) << 32) | (uint32_t) c[1]) typedef std::string::value_type char_t; char_t upcase(char_t ch) { return std::use_facet<std::ctype<char_t>>(std::locale()).toupper(ch); } std::string toupper(const std::string &src) { std::string result; std::transform(src.begin(), src.end(), std::back_inserter(result), upcase); return result; } const uint64_t VOWS_EXTRA[] { E("å") , E("ä"), E("ö"), E("ij"), E("ø"), E("æ") }; int main(void) { char name[5]; std::locale::global(std::locale("sv_SE.UTF8")); name[0] = (VOWS_EXTRA[3] >> 32) & ~((uint32_t)0); name[1] = VOWS_EXTRA[3] & ~((uint32_t)0); name[2] = '\0'; std::cout << toupper(name) << std::endl; } #包括 #包括 #包括 #包括 #包括。似乎我的最佳选择是使用ICU或Boost.locale之类的工具来完成此任务。C++基本上将STD::string作为二进制数据的一个块,因此对于大写的Unicode字母来说似乎不是一个简单的任务。我认为我的Huky解决方案使用UIT64 64 T并没有比C++标准库更有用,如果不是更糟的话。如果能举一个例子说明如何使用ICU实现上述行为,我将不胜感激。

如何在C++; 我正在学习C++中的Unicode,而且很难使它正常工作。我试图将单个字符视为uint64\t。如果我只需要打印出字符,它就可以工作,但问题是我需要将它们转换为大写。我可以将大写字母存储在一个数组中,并简单地使用与小写字母相同的索引,但我正在寻找一个更优雅的解决方案。我发现这很相似,但大多数答案都使用了宽字符,这是我无法使用的。以下是我的尝试: #include <iostream> #include <locale> #include <string> #include <cstdint> #include <algorithm> // hacky solution to store a multibyte character in a uint64_t #define E(c) ((((uint64_t) 0 | (uint32_t) c[0]) << 32) | (uint32_t) c[1]) typedef std::string::value_type char_t; char_t upcase(char_t ch) { return std::use_facet<std::ctype<char_t>>(std::locale()).toupper(ch); } std::string toupper(const std::string &src) { std::string result; std::transform(src.begin(), src.end(), std::back_inserter(result), upcase); return result; } const uint64_t VOWS_EXTRA[] { E("å") , E("ä"), E("ö"), E("ij"), E("ø"), E("æ") }; int main(void) { char name[5]; std::locale::global(std::locale("sv_SE.UTF8")); name[0] = (VOWS_EXTRA[3] >> 32) & ~((uint32_t)0); name[1] = VOWS_EXTRA[3] & ~((uint32_t)0); name[2] = '\0'; std::cout << toupper(name) << std::endl; } #包括 #包括 #包括 #包括 #包括。似乎我的最佳选择是使用ICU或Boost.locale之类的工具来完成此任务。C++基本上将STD::string作为二进制数据的一个块,因此对于大写的Unicode字母来说似乎不是一个简单的任务。我认为我的Huky解决方案使用UIT64 64 T并没有比C++标准库更有用,如果不是更糟的话。如果能举一个例子说明如何使用ICU实现上述行为,我将不胜感激。,c++,c++11,unicode,locale,uppercase,C++,C++11,Unicode,Locale,Uppercase,请看下面的例子。对于简单(单字符)大小写映射,可以使用。对于完整案例映射,请使用。示例代码: #include <unicode/uchar.h> #include <unicode/ustdio.h> #include <unicode/ustring.h> int main() { UChar32 upper = u_toupper(U'ij'); u_printf("%lC\n", upper); UChar src = u'ß

请看下面的例子。对于简单(单字符)大小写映射,可以使用。对于完整案例映射,请使用。示例代码:

#include <unicode/uchar.h>
#include <unicode/ustdio.h>
#include <unicode/ustring.h>

int main() {
    UChar32 upper = u_toupper(U'ij');
    u_printf("%lC\n", upper);

    UChar src = u'ß';
    UChar dest[3];
    UErrorCode err = U_ZERO_ERROR;
    u_strToUpper(dest, 3, &src, 1, NULL, &err);
    u_printf("%S\n", dest);

    return 0;
}
#包括
#包括
#包括
int main(){
UChar32 upper=uijtoupper(uij’);
u_printf(“%lC\n”,大写);
UChar src=u'ß';
UChar dest[3];
UErrorCode err=U_零_错误;
u_strotupper(dest、3和src、1、NULL和err);
u_printf(“%S\n”,dest);
返回0;
}

另外,如果有人在找它,
std::towupper
std::towlower
似乎也能正常工作

请不要假装Unicode是固定宽度编码。@Nicolas抱歉,我对Unicode非常缺乏经验,我尝试使用常规字符串,但无法使其与单个字符一起使用。
std::locale::global(std::locale(“sv_SE.UTF8”)
与Windows不兼容,除非您使用非常特殊的编译器。Microsoft的运行时不支持UTF-8区域设置。请参阅
setlocale
的文档。要包含UTF-8文本,只需使用例如
u“哦,这么简单吗?”
。主要的问题是基本字符类型仍然是
char
。完整Unicode的大小写通常不能逐个字符进行。有时,在相反的情况下,单个字符映射为两个字符。我认为对于希腊语来说,这取决于字符在一个单词中的位置,或者在单词的结尾(或者是开始?)。对于真正迂腐的人来说,它甚至不能以独立于语言环境的方式完成(这是土耳其语的一个特殊问题),但我认为几乎所有软件都忽略了这一点。谢谢,对于迟来接受的答案,我感到抱歉。我花了几个小时才让重症监护室开始工作。我有很多关于“未定义函数引用”错误的问题。