I am holding hex values in unsigned integers size_t
and would like to convert them into wchar_t
to hold in a data-structure and, optionally print to std::cout
as it's UTF-8 symbol/character when valid.
I've tried casting without much success: size_t h = 0x262E;
prints 9774
when doing a cast to wchar_t
for example.
Some minimal code:
#include <iostream>
#include <vector>
int main() {
std::setlocale( LC_ALL, "" );
auto v = std::vector<size_t>( 3, 0x262E ); // 3x peace symbols
v.at( 1 ) += 0x10; // now a moon symbol
for( auto &el : v )
std::cout << el << " ";
return 0;
}
Output: 9774 9790 9774
What I want: ☮ ☾ ☮
I can print the symbols using printf( "%lc ", (wchar_t) el );
. Is there a better "modern" C++ solution to this?
I need to be able to print anything in the range of 0000-27BF
UTF-8 on linux only .
You need std::wcout
with wchar_t
cast to print the wide characters instead of std::cout
.
Here's your corrected functional code ( live example ):
#include <iostream>
#include <vector>
int main() {
std::setlocale( LC_ALL, "" );
auto v = std::vector<size_t>( 3, 0x262E ); // 3x peace symbols
v.at( 1 ) += 0x10; // now a moon symbol
for( auto &el : v )
std::wcout << (wchar_t) el << " "; // <--- Corrected statement
return 0;
}
Output:
☮ ☾ ☮
In case you have hex string numbers, you can follow this solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.