簡體   English   中英

在 C++20 中將 wchar_t * 轉換為 char *

[英]Converting wchar_t * to char * in C++20

目前使用 WSL2 Ubuntu,G++20。

在 C++20 中將 wchar_t * 轉換為 char * 的一些推薦方法是什么? 我看過幾年前創建的類似帖子,但我不確定它們作為解決方案是否仍然可行,或者它們是否已被棄用。

如果我沒記錯的話wcstombs (和wcsrtombs )在 C++ 20 中仍然相關:

#include <iostream>
#include <clocale>
#include <cstdlib>
 
int main()
{
    std::setlocale(LC_ALL, "en_US.utf8");
    // UTF-8 narrow multibyte encoding
    const wchar_t* wstr = L"z\u00df\u6c34\U0001d10b"; // or L"zß水𝄋"
    char mbstr[11];
    std::wcstombs(mbstr, wstr, 11);
    std::cout << "multibyte string: " << mbstr << '\n';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM