2

My variable is in a specific internal implementation of a wide string but I can get it to be a wide character array quite easily. The problem is I need to feed it to a library function that will only accept character arrays.

So I end up with what looks like this:

wchar_t* wstr;

// char* str = "need some processing here";

function(str);

So, is there an easy way or should I just do it manually character by character?

obmarg
  • 9,369
  • 36
  • 59
  • 2
    It depends a bit of what you are trying to do with the result and what the original contains. If you just want to process ASCII characters, you'd want `std::ctype::narrow()`. If the function expects a muti-byte encoding something like `wcstombs()` is needed. Note, that the latter are typically not considered characters but a sequence of byte. – Dietmar Kühl Sep 24 '12 at 18:21
  • This seems to be working for me although I found a better solution to my problem that doesn't require this conversion. But thanks, anyway. – Sabre Runner Sep 27 '12 at 11:25

1 Answers1

2

char* can mean a lot of things. If the function taking the char* is actually taking UTF8, there are ways to transform UTF16 or UTF32 to UTF8. wchar_t is commonly used to store UTF16 (windows) or UTF32 (macosx), amongst other enencodings.

Have a look here for ConvertUTF.c/.h

unicode.org code

cppguy
  • 3,611
  • 2
  • 21
  • 36
  • How can I know if it takes UTF-8 or not? – Sabre Runner Sep 27 '12 at 10:17
  • 1
    Simply put, UTF8 is one of many ways to encode a char array to represent most characters/letters/symbols/etc... of most languages. It is probably the most popular. If you are calling into a function defined by some library, check the documentation to see what kind of char* it expects. Many popular APIs expect a char* to be UTF8. If you know that your wchar_t input will only ever be English characters (ascii codes 32 to 127), then you can transform the wchar_t to char* using the above mentioned library because those characters are valid in all char* encoding schemes. – cppguy Sep 27 '12 at 17:11