将'const wchar_t *'转换为'unsigned char *'

时间:2021-12-31 05:53:07

In C++ is it possible to convert a 'const wchar_t *' to 'unsigned char *'?

在c++中,是否可以将“const wchar_t *”转换为“unsigned char *”?

How can I do that?

我该怎么做呢?

wstring dirName;
unsigned char* dirNameA = (unsigned char*)dirName.c_str();

// I am creating a hash from a string
hmac_sha256_init( hash, (unsigned char*)dirName.c_str(), (dirName.length)+1 );

3 个解决方案

#1


1  

You need to convert character by character. There are functions like wcstombs to do this.

你需要按字符转换字符。有像wcstombs这样的功能。

#2


1  

Try using reinterpret_cast. So:

试着用reinterpret_cast。所以:

unsigned char * dirNameA = reinterpret_cast<unsigned char *>(dirName.c_str());

That might not work because c_str returns a const wchar_t *so you can also try:

这可能不起作用,因为c_str返回一个const wchar_t *,因此您也可以尝试:

unsigned char * dirNameA = reinterpret_cast<unsigned char *>(
                               const_cast<wchar_t *>(dirName.c_str())
                           );

This works because hmac_sha256_init should accept a binary blob as its input, so the unicode string contained in dirName is an acceptable hash input.

这是因为hmac_sha256_init应该接受二进制blob作为输入,因此dirName中包含的unicode字符串是一个可接受的散列输入。

But there's a bug in your code - the length returned by dirName.length() is a count of characters, not a count of bytes. That means that passing too few bytes to hmac_sha256_init since you're passing in a unicode string as a binary blob, so you need to multiply (dirName.length()) by 2.

但是代码中有一个bug——由dirName.length()返回的长度是字符数,而不是字节数。这意味着,由于将unicode字符串作为二进制blob传递给hmac_sha256_init的字节数太少,所以需要将(dirName.length())乘以2。

#3


1  

Since you're using WinAPI, use WideCharToMultiByte.

因为您使用的是WinAPI,所以使用WideCharToMultiByte。

#1


1  

You need to convert character by character. There are functions like wcstombs to do this.

你需要按字符转换字符。有像wcstombs这样的功能。

#2


1  

Try using reinterpret_cast. So:

试着用reinterpret_cast。所以:

unsigned char * dirNameA = reinterpret_cast<unsigned char *>(dirName.c_str());

That might not work because c_str returns a const wchar_t *so you can also try:

这可能不起作用,因为c_str返回一个const wchar_t *,因此您也可以尝试:

unsigned char * dirNameA = reinterpret_cast<unsigned char *>(
                               const_cast<wchar_t *>(dirName.c_str())
                           );

This works because hmac_sha256_init should accept a binary blob as its input, so the unicode string contained in dirName is an acceptable hash input.

这是因为hmac_sha256_init应该接受二进制blob作为输入,因此dirName中包含的unicode字符串是一个可接受的散列输入。

But there's a bug in your code - the length returned by dirName.length() is a count of characters, not a count of bytes. That means that passing too few bytes to hmac_sha256_init since you're passing in a unicode string as a binary blob, so you need to multiply (dirName.length()) by 2.

但是代码中有一个bug——由dirName.length()返回的长度是字符数,而不是字节数。这意味着,由于将unicode字符串作为二进制blob传递给hmac_sha256_init的字节数太少,所以需要将(dirName.length())乘以2。

#3


1  

Since you're using WinAPI, use WideCharToMultiByte.

因为您使用的是WinAPI,所以使用WideCharToMultiByte。