Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
Anonymous
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#include <iostream> #include <string> #include <windows.h> template<typename A, typename T> inline ::std::basic_string<A> uni_cast (const T* source) { std::cout << "uni_cast <T, T>" << std::endl; return (source); } template<> inline ::std::basic_string<char> uni_cast (const wchar_t* source) { std::cout << "uni_cast <char, wchar_t>" << std::endl; char *buffer = new char[::wcslen (source)*2]; ::WideCharToMultiByte (CP_ACP, 0, source, -1, buffer, static_cast<int>(::wcslen (source)*2), NULL, NULL); std::basic_string<char> result (buffer); delete [] buffer; return (result); } template<> inline ::std::basic_string<wchar_t> uni_cast (const char* source) { std::cout << "uni_cast <wchar_t, char>" << std::endl; wchar_t *buffer = new wchar_t[::strlen (source)]; ::MultiByteToWideChar (CP_ACP, 0, source, -1, buffer, static_cast<int>(::strlen (source))); std::basic_string<wchar_t> result (buffer); delete [] buffer; return (result); } int main (void) { std::basic_string<wchar_t> test = uni_cast<wchar_t>("Hallo!"); // ANSI zu Unicode std::basic_string<char> test2 = uni_cast<char>(L"Hallo!"); // Unicode zu ANSI std::basic_string<char> test3 = uni_cast<char>("Hallo!"); // ANSI zu ANSI std::basic_string<wchar_t> test4 = uni_cast<wchar_t>(L"Hallo!"); // Unicode zu Unicode std::wcout << test << std::endl; std::cout << test2 << std::endl; std::cout << test3 << std::endl; std::wcout << test4 << std::endl; std::cin.get(); return 0; } |
Anonymous
unregistriert
Sicaine
unregistriert
Zitat von »"nix da"«
koschka
Die ist doch nur zur Verdeutlichung da, dass andere sehen wie er funktioniert. Den lass ich doch nicht freiwillig da drin
Zitat von »"Deviloper"«
bäääh ... wenigstens nen schöne plattformunabhängige Lösung über std::codecvt wäre wünschenswert gewesen (ok über std::ctype ginge notfalls auch ... deprected) ...
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <string> #include <local> #include <vector> template<class dest_type, class source_type> inline const std::basic_string<dest_type> char_cast(const std::basic_string<source_type>& source, const std::locale& loc = std::locale::classic()) { std::vector<dest_type> result (source.length(), 0); mbstate_t state; const source_type* ptr_source_next; dest_type* ptr_dest_next; std::use_facet<std::codecvt<dest_type, source_type, mbstate_t> >(loc).in(state, &source[0], &source[0] + source.length(), ptr_source_next, &result[0], &result[0] + result.size(), ptr_dest_next); return std::basic_string<dest_type>(result.begin(), result.end()); } template<> inline const std::basic_string<char> char_cast(const std::basic_string<char>& source, const std::locale&) { return source; } template<> inline const std::basic_string<wchar_t> char_cast(const std::basic_string<wchar_t>& source, const std::locale&) { return source; } |
Werbeanzeige