0

Is there a elegant way to convert 'const wchar *' to 'const char *' on Mac OS X?

Kat

Nifle
  • 11,745
  • 10
  • 75
  • 100
kyue
  • 143
  • 3
  • 8

2 Answers2

2

Use wcstombs:

size_t wcstombs(char *dest, const wchar_t *src, size_t n);

Make sure you have your locale set appropriately.

ankon
  • 4,128
  • 2
  • 26
  • 26
0

It's hard to imagine why one would be using wchar_t on OS X, but here's how it can be done in standard C++:

#include <codecvt>

int main() {
    std::wstring_convert<std::codecvt_utf8<wchar_t>,wchar_t> convert;

    wchar_t const *ws = L"Steve Nash";
    std::string s = convert.to_bytes(ws);
    char const *cs = s.c_str();
}
bames53
  • 86,085
  • 15
  • 179
  • 244