转为 unicode 或不转为 unicode
我从注册表中获取一个值。该值中可能包含双字节字符。 稍后我必须通过网络将其传输到 C# 客户端进行显示。 C# 都是unicode。 如果您调用非 unicode,该函数将返回 MBCS。
我应该用什么?
string result = string(cbData);
RegQueryValueExA(h_sub_key, "DisplayName", NULL, NULL, (LPBYTE) &result[0], &cbData)
或者
string result = string(cbData);
RegQueryValueExW(h_sub_key, L"DisplayName", NULL, NULL, (LPBYTE) &result[0], &cbData)
I'm getting a value from the registry. This value might have double byte characters in it.
I will later have to transfer this across the network to a C# client to display. C# is all unicode.
The function returns MBCS if you call it non-unicode.
What should I use?
string result = string(cbData);
RegQueryValueExA(h_sub_key, "DisplayName", NULL, NULL, (LPBYTE) &result[0], &cbData)
or
string result = string(cbData);
RegQueryValueExW(h_sub_key, L"DisplayName", NULL, NULL, (LPBYTE) &result[0], &cbData)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尽可能使用 Unicode 将使您的生活更轻松。注册表本身包含 Unicode,并在您使用 ReqQueryValueExA 时即时转换为 MBCS,为什么要进行不必要的转换?
对于通过网络传输的信息,从 UTF-16 转换为 UTF-8 可能有意义,但如果您控制连接的两端,则没有必要。
Using Unicode whenever possible will make your life easier. The registry contains Unicode natively and converts to MBCS on the fly when you use ReqQueryValueExA, why would you want to do an unneeded conversion?
Converting to UTF-8 from UTF-16 might make sense for information going over the network, but if you control both ends of the connection it wouldn't be necessary.
不,事情不是这样的。从第一个片段返回的字符串是根据当前系统代码页进行编码的。可能是双字节编码。可以是任何东西。当然,大问题是,Internet 连接另一端的 C# 代码无法猜测代码页可能是什么。
所以不要使用第一个片段。第二个获取 utf16 格式的字符串,这是 Windows 中使用的本机编码,结果 需要是 std::wstring。还有 C# 使用的编码,以便您可以发送二进制字符串。虽然这通常不是一个好主意,但 xml 很流行。由您来设置有线格式。
No, that's not the way it works. The string you get back from the first snippet is encoded according to the current system code page. Could be a double-byte encoding. Could be anything. Big problem of course, the C# code on the other end of that Internet connection has no way to guess what the code page might be.
So do not use the first snippet. The second one gets you the string in utf16, the native encoding used in Windows, result needs to be an std::wstring. Also the encoding used by C# so you could send the binary string. Although that's not typically a good idea, xml is popular. It is up to you to set the wire format.