gpt4 book ai didi

带有 wxWidgets 的 C++,Unicode 与 ASCII,有什么区别?

转载 作者:行者123 更新时间:2023-11-30 02:07:57 25 4
gpt4 key购买 nike

我有一个 Code::Blocks 10.05 rev 0 和 gcc 4.5.2 Linux/unicode 64bit 和WxWidgets 版本 2.8.12.0-0
我有一个简单的问题:

#define _TT(x) wxT(x)
string file_procstatus;
file_procstatus.assign("/PATH/TO/FILE");
printf("%s",file_procstatus.c_str());
wxLogVerbose(_TT("%s"),file_procstatus.c_str());

Printf 正常输出“/PATH/TO/FILE”,而 wxLogVerbose 变成垃圾。当我想将 std::string 更改为 wxString 时,我必须执行以下操作:

wxString buf;
buf = wxString::From8BitData(file_procstatus.c_str());

有人知道可能出了什么问题,为什么我需要更改 8 位数据?

最佳答案

这与字符数据在内存中的存储方式有关。使用“string”,您使用 ASCII 字符集生成 char 类型的字符串,而我假设 _TT 宏扩展为 L"string",它创建一个字符串使用 Unicode 字符集(我相信 Linux 上的 UTF-32)键入 wchar_t

printf 函数需要一个 char 字符串,而 wxLogVerbose 我假设需要一个 wchar_t 字符串。这就是转换需求的来源。 ASCII 每个字符使用一个字节(8 位数据),但 wchar_t 字符串每个字符使用多个字节,因此问题归结为字符编码。

如果您不想调用此转换函数,请执行以下操作:

wstring file_procstatus = wxT("/PATH/TO/FILE");
wxLogVerbose(_TT("%s"),file_procstatus.c_str());

关于带有 wxWidgets 的 C++,Unicode 与 ASCII,有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com