0

I have a program that is supposed to run on all platfotms. So I need to get rid of wchar_t and use char16_t instead. How can I convert wchar_t <-> char16_t For example how would I get to use wsclen() here?

wchar_t WT = L'Hello World! of WCHART';
wchar_t* buf = new wchar_t[234];
int a = wcslen(buf);
int aa = wcslen(&WT);

char16_t W16[] = u"Hello World! of CHAR16";
int b = wcslen( W16);                           <--- ERROR HERE
wohlstad
  • 12,661
  • 10
  • 26
  • 39
user3443063
  • 1,455
  • 4
  • 23
  • 37
  • 3
    Unlike `char16_t`, `wchar`t` has different widths on different platforms. Like using 16 bits on Windows and 32 bits on Linux. That makes any conversions troublesome. For finding the string length you might be able to use `std::char_traits::length`. – BoP Feb 22 '22 at 09:47
  • `wchar_t WT = L'Hello World! of WCHART'`, looks like you are trying to define a single character and initialize it with a string? Probably not what you want to do? – hyde Feb 22 '22 at 10:05
  • Duplicate of https://stackoverflow.com/questions/7232710/convert-between-string-u16string-u32string ? `that is supposed to run on all platfotms. So I need to get rid of wchar_t and use char16_t instead` that is an odd assumption. How did you end up with that conclusion? `wchar_t` should be more portable than `char16_t`. Don't use both and stick to `char` and multi-byte strings. – KamilCuk Feb 22 '22 at 10:20
  • Best way to solve this issue is to use a framework like Qt which deals string encoding for you. It's completely cross platform. The class QString has static members to create strings from different encoding and to translate them too – Marco Beninca Feb 22 '22 at 13:28

0 Answers0