简体   繁体   English

从 WCHAR 转换为 const unsigned char

[英]Conversion from WCHAR to const unsigned char

I am a wix application packager.我是 wix 应用程序打包者。 I am quite new to C++ and I am stuck with the below issue.我对 C++ 很陌生,我被以下问题困扰。

In my code, I am trying to convert wchar to const unsigned char .在我的代码中,我试图将wchar转换为const unsigned char I have tried quite a few solutions that I got on the Internet, but I am unable to do it.我已经尝试了很多我在互联网上获得的解决方案,但我无法做到。

WCHAR szabc[888] = L"Example";

const unsigned char* pText = (const unsigned char*)szabc;

For your reference, the value of szabc is hard-coded, but ideally it is fetched as user input during installation of my code.供您参考, szabc的值是硬编码的,但理想情况下,它是在安装我的代码期间作为用户输入获取的。 szabc needs to be converted to const unsigned char as operator= doesn't seem to be working for conversion. szabc需要转换为const unsigned char因为operator=似乎不适用于转换。

I am not getting any compilation error, but when I run this code, only the first character of szabc is being assigned to pText , I want the whole value of szabc to be assigned to pText .我没有收到任何编译错误,但是当我运行此代码时,仅将szabc的第一个字符分配给pText ,我希望将szabc的整个值分配给pText

As the value of pText is a user account password in a real time scenario, and it will be passed to a method which encrypts the value of the password.由于pText的值是实时场景中的用户帐户密码,它会被传递给对密码值进行加密的方法。

Since you neglected to mention your OS, I am assuming it is Windows.由于您忽略了提及您的操作系统,我假设它是 Windows。 You need WideCharToMultiByte or the standard wcstombs functions.您需要WideCharToMultiByte或标准wcstombs函数。

Note that both will determine the target encoding using system settings, so results will vary across computers.请注意,两者都将使用系统设置确定目标编码,因此结果因计算机而异。 If possible, convert to UTF-8 or tell your users to stay away from special characters.如果可能,请转换为 UTF-8 或告诉您的用户远离特殊字符。

operator= cannot assign a value to a variable of an unrelated type. operator=不能为不相关类型的变量赋值。 Which is why you cannot assign a WCHAR[] directly to an unsigned char* .这就是为什么您不能将WCHAR[]直接分配给unsigned char*的原因。

However, the real problem is with how the pointed data is being interpreted .然而,真正的问题在于如何解释指向的数据。 You have a 16-bit Unicode string, and you are trying to pass it to a method that clearly wants a null-terminated 8-bit string instead.您有一个 16 位 Unicode 字符串,并且您正试图将它传递给一个明显需要以空字符结尾的 8 位字符串的方法。

On Windows, WCHAR is 2 bytes, and so the 2nd byte in your Unicode string is 0x00 , eg:在 Windows 上, WCHAR是 2 个字节,因此 Unicode 字符串中的第二个字节0x00 ,例如:

WCHAR szabc[] = {L'E', L'x', L'a', L'm', L'p', L'l', L'e', L'\0'};

Has the same memory layout as this:具有与此相同的 memory 布局:

BYTE szabc[] = {'E', 0x00, 'x', 0x00, 'a', 0x00, 'm', 0x00, 'p', 0x00, 'l', 0x00, 'e', 0x00, '\0', 0x00};

This is why the method appears to see only 1 "character".这就是该方法似乎只看到 1 个“字符”的原因。 It stops reading when it encounters the 1st 0x00 byte.它在遇到第一个0x00字节时停止读取。

Thus, a simple pointer type-cast will not suffice.因此,简单的指针类型转换是不够的。 You will need to either:您将需要:

  • use an 8-bit string to begin with, eg:使用 8 位字符串开头,例如:

     CHAR szabc[888] = "Example"; unsigned char* pText = (unsigned char*)szabc; // use pText as needed...
  • convert the Unicode data at runtime, using WideCharToMultiByte() or equivalent, eg:使用WideCharToMultiByte()或等效方法在运行时转换Unicode 数据,例如:

     WCHAR szabc[888] = L"Example"; int len = WideCharToMultiByte(CP_ACP, 0, szabc, -1, NULL, 0, NULL, NULL); CHAR szConverted = new char[len]; WideCharToMultiByte(CP_ACP, 0, szabc, -1, szConverted, len, NULL, NULL); unsigned char* pText = (unsigned char*)szConverted; // use pText as needed... delete[] szConverted;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM