How to use wcstombs instead of WideCharToMultiByte

2020-07-26 15:07发布

wcstombs have three args.

WideCharToMultiByte have eight args.,

How to replace?

How use wcstombs write like that :

int kk = WideCharToMultiByte(936, 0, szBn, ccBn+1, 0, 0, 0, 0);

How use wcstombs write like that :

int kk= WideCharToMultiByte(936,
                            0,
                            szBn,
                            ccBn+1,
                            kkburr,
                            (ccBn+1)*sizeof(wchar_t),
                            0,
                            0)

Does it same?

标签: c++
2条回答
smile是对你的礼貌
2楼-- · 2020-07-26 15:37

WideCharToMultiByte does something much different than wcstombs, so it's at least somewhat doubtful¹ that this replacement would be successful.

Specifically, WCTMB converts from UTF-16 to another character set (and encoding) of your choice. wcstombs converts from a "wide character string" to a "multibyte string", but the standard leaves the exact definition of these terms to the implementor.

In other words: WCTMB converts from a known encoding to another known encoding. wcstombs converts from an unknown encoding to another unknown encoding. You cannot replace the former with the latter, especially if you want to convert to a Chinese encoding (isn't that what 936 is?).

What's the reason for seeking to do this replacement? Whatever it may be, it's almost certain that there are more appropriate ways to achieve the goal.

Update: If you want a platform-independent, reliable solution then I 'd recommend ICU. Don't expect to find an one-line replacement for WCTMB because it simply doesn't exist.


¹ massive understatement

查看更多
Rolldiameter
3楼-- · 2020-07-26 15:39
  • The resulting encoding that wcstombs converts to depends on the current locale while WideCharToMultiByte can be explicitly given a codepage.
  • wcstombs converts from "wide character string", which encoding depends on the implementation of the standard library. WideCharToMultiByte converts from the string in UTF-16. Wide characters wcstombs rely upon may not be encoded in UTF-16, but in Windows wide characters are UTF-16.
  • wcstombs is portable, WideCharToMultiByte is not. If your target platform is Windows only then it doesn't matter.
  • WideCharToMultiByte allows to specify a "default character" which will be used instead of characters that can't be represented in target encoding.

So WideCharToMultiByte is much more powerful and is probably used as a backend for wcstombs.

If your current codepage is not 936, then the replacement you ask about is impossible.

查看更多
登录 后发表回答