   string to wchar_t *
  Hi! All,I'm trying to load image using DevIL. Please give me the solution for this method...wchar_t *getWChars(string lines){   return NULL;}Please help. :(   
/* Make enough space to hold the string + the zero element */wchar_t* wstr = new wchar_t[lines.length()+1];/* Convert the char array to wchar array */MultiByteToWideChar(CP_ACP,0,lines.c_str(),-1,wstr,(int)lines.length());/* Set the last element 0 */wstr[lines.length()] = 0;return wstr;You must include windows.h in order to use MultiByteToWideChar .   ;  I'd recommend doing something like:using std::wstring;wstring getWChars(const string& lines){    wstring wlines;    wlines.resize(lines.length());    wlines.assign(lines.begin(), lines.end());    return wlines;}// ... and then use it likewstring wstr(getWChars(lines));doSomethingWithAwchar_t(wstr.c_str());This avoids using the platform specific MultiByteToWideChar call, and does all of the memory allocation/deallocation for you.  Refactor to taste.   ;  Or even justdoSomethingWithAwchar_t(std::wstring(string.begin(), string.end()).c_str());Σnigma   ;Quote:Original post by EnigmaOr even justdoSomethingWithAwchar_t(std::wstring(string.begin(), string.end()).c_str());ΣnigmaGood call :)   ;Quote:Original post by godechoI'd recommend doing something like:*** Source Snippet Removed ***This avoids using the platform specific MultiByteToWideChar call, and does all of the memory allocation/deallocation for you.  Refactor to taste.Your method doesn't work for anything other than ASCII.I believe you can use widen to convert from char strings to wchar_t strings if you need portability.   ;  just use standard function mbstowcs:http://www.cplusplus.com/reference/clibrary/cstdlib/mbstowcs.htmlhttp://msdn.microsoft.com/en-us/library/k1f9b8cy(VS.80).aspx   #include<string>   #include<stdio.h> void main()   {    std::string s1("hello world");    const char *cstr = s1.c_str();    wchar_t *s2 = new wchar_t[s1.size()];    mbstowcs(s2,s1.c_str(),s1.size());    wprintf(s2);    delete s2;   } runs without glitches with gcc and vs.Since unicode is "backward compatible" with ascii, you could even do a dirty trick like this:#include <string>#include <stdio.h>int main(){  std::string stdstr("Hello World");  const char *cstr =  stdstr.c_str();  int l = stdstr.size();  wchar_t s[12];    for(int i = 0; i < l; i++)    s = (wchar_t)cstr;    printf("ASCII: %s\n",cstr);  wprintf(L"UNICODE: %s\n",s);  return 0;} (Obs: works only if you have ascii symbols in your string, I don't know how good it works if you put some other symbols as found in extended ascii tables (for example: Ääöå and alike).   ;Quote:Original post by joe_bubamarajust use standard function mbstowcs:http://www.cplusplus.com/reference/clibrary/cstdlib/mbstowcs.htmlhttp://msdn.microsoft.com/en-us/library/k1f9b8cy(VS.80).aspx   #include<string>   #include<stdio.h> void main()   {    std::string s1("hello world");    const char *cstr = s1.c_str();    wchar_t *s2 = new wchar_t[s1.size()];    mbstowcs(s2,s1.c_str(),s1.size());    wprintf(s2);    delete s2;   } runs without glitches with gcc and vs.Since unicode is "backward compatible" with ascii, you could even do a dirty trick like this:#include <string>#include <stdio.h>int main(){  std::string stdstr("Hello World");  const char *cstr =  stdstr.c_str();  int l = stdstr.size();  wchar_t s[12];    for(int i = 0; i < l; i++)    s = (wchar_t)cstr;    printf("ASCII: %s\n",cstr);  wprintf(L"UNICODE: %s\n",s);  return 0;} (Obs: works only if you have ascii symbols in your string, I don't know how good it works if you put some other symbols as found in extended ascii tables (for example: Ääöå and alike).And the memory leak / crash (if you're unlucky) in your code demonstrates perfectly why you should use std::wstring.   ;Quote:Original post by joe_bubamaraSince unicode is "backward compatible" with asciiNo. UTF-8 is "ASCII transparent". UTF-8 is not "Unicode". It's one of many Unicode encodings. UTF-16 (which wchar_t normally is) only works as shown because every other byte is a zero byte for those first few characters. But even then, you have to be aware of endianness.Quote:you could even do a dirty trick like this:If the limitations are met, and you remember to null-terminate the wchar_t buffer, I suppose you could. But why would you take more work upon yourself, and more risk of making everything blow up, when the functionality is already right there for you (as illustrated by godecho and Enigma)?;Quote:Original post by joe_bubamara   #include<string>   #include<stdio.h> void main()   {    std::string s1("hello world");    const char *cstr = s1.c_str();    wchar_t *s2 = new wchar_t[s1.size()];    mbstowcs(s2,s1.c_str(),s1.size());    wprintf(s2);    delete s2;   } always use delete[] when freeing a dynamically allocated array.delete[] s2;Quote:Original post by ZahlmanAs a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.using string to avoid crashes just proves that you don't know how to handle your own memory allocation, and if so you should never touch the 'new' function at all.   