我需要使用具有以下 ascii 值的字符创建一个 wstring:30、29、28、27、26、25。
在 VB6 中,我会做 asc(30) + asc(29)+ 等...
什么是 C++ 等价物?
谢谢!
最佳答案
这是关于字符集转换的技巧问题吗? :) 因为该标准不保证 ASCII 字符由其在 wchar_t 中的 ASCII 整数值表示(即使对于大多数编译器/系统而言,这也是正确的)。如果重要,请使用适当的语言环境明确扩大您的字符:
std::wstring s; std::locale loc("C"); // pick a locale with ASCII encoding s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(30)); s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(29)); s.push_back(std::use_facet<std::ctype<wchar_t> >(loc).widen(28));
不要以尾随 0 结尾,它是在您通过调用 .c_str() 将 wstring 转换为 wchar_t * 时添加的
关于c++ - 如何在 C++ 中使用字符的 ascii 代码创建 std::wstring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3459445/