如何在 Linux API 中使用 wstring?
我想在 Linux 上开发一个应用程序。我想使用 wstring 因为我的应用程序应该支持 unicode 并且我不想使用 UTF-8 字符串。
在Windows操作系统中,使用wstring很容易。因为任何 ANSI API 都有 unicode 形式。例如有两个CreateProcess API,第一个API是CreateProcessA,第二个API是CreateProcessW。
wstring app = L"C:\\test.exe";
CreateProcess
(
app.c_str(), // EASY!
....
);
但在 Linux 中使用 wstring 似乎很复杂!例如,Linux 中有一个名为 parport_open(这只是一个例子)。
我不知道如何将我的 wstring 发送到此 API(或像 parport_open 这样接受字符串参数的 API)。
wstring name = L"myname";
parport_open
(
0, // or a valid number. It is not important in this question.
name.c_str(), // Error: because type of this parameter is char* not wchat_t*
....
);
我的问题是如何在 Linux API 中使用 wstring?
注意:我不想使用 UTF-8 字符串。
谢谢
I want to develope an application in Linux. I want to use wstring beacuse my application should supports unicode and I don't want to use UTF-8 strings.
In Windows OS, using wstring is easy. beacuse any ANSI API has a unicode form. for example there are two CreateProcess API, first API is CreateProcessA and second API is CreateProcessW.
wstring app = L"C:\\test.exe";
CreateProcess
(
app.c_str(), // EASY!
....
);
But it seems working with wstring in Linux is complicated! for example there is an API in Linux called parport_open (It just an example).
and I don't know how to send my wstring to this API (or APIs like parport_open that accept a string parameter).
wstring name = L"myname";
parport_open
(
0, // or a valid number. It is not important in this question.
name.c_str(), // Error: because type of this parameter is char* not wchat_t*
....
);
My question is how can I use wstring(s) in Linux APIs?
Note: I don't want to use UTF-8 strings.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几乎每个发行版上的 Linux API(在最新的内核上并具有正确的区域设置)默认都使用 UTF-8 字符串1。您也应该在代码中使用它们。抵抗是徒劳的。
Windows 上的
wchar_t
(以及wstring
)仅在 Unicode 限制为 65536 个字符时才方便(即wchar_t
用于 UCS-2) ,现在 16 位 Windowswchar_t
用于 UTF-16,1wchar_t
=1 Unicode 字符的优势早已不复存在,因此您拥有使用 UTF-8 也有同样的缺点。恕我直言,现在 Linux 方法是最正确的。 (我关于 UTF-16 的另一个答案以及为什么 Windows 和 Java 使用它)顺便说一下,
string
和wstring
都无法识别编码,因此您无法可靠地使用这两个中的任何一个操作 Unicode 代码点。我听说 wxWidgets 工具包中的wxString
可以很好地处理 UTF-8,但我从未对此进行过广泛的研究。Linux APIs (on recent kernels and with correct locale setting) on almost every distribution use UTF-8 strings by default1. You too should use them inside your code. Resistance is futile.
The
wchar_t
(and thuswstring
) on Windows were convenient only when Unicode was limited to 65536 characters (i.e.wchar_t
were used for UCS-2), now that the 16-bit Windowswchar_t
are used for UTF-16 the advantage of 1wchar_t
=1 Unicode character is long gone, so you have the same disadvantages of using UTF-8. Nowadays IMHO the Linux approach is the most correct. (Another answer of mine on UTF-16 and why Windows and Java use it)By the way, both
string
andwstring
aren't encoding-aware, so you can't reliably use any of these two to manipulate Unicode code points. I heard thatwxString
from the wxWidgets toolkit handles UTF-8 nicely, but I never did extensive research about it.那么,您需要克服这种不情愿,至少在调用 API 时是这样。 Linux 使用单字节字符串编码,始终为 UTF-8。显然,您应该使用单字节字符串类型,因为您显然无法将宽字符传递给需要
char*
的函数。使用string
而不是wstring
。Well, you will need to overcome that reluctance, at least when calling the APIs. Linux uses single byte string encodings, invariably UTF-8. Clearly you should use a single byte string type since you obviously can't pass wide characters to a function that expects
char*
. Usestring
rather thanwstring
.