google-site-verification: googlebaca44933768a824.html Converting std::string to TCHAR and vice verse - Old Royal Hack Forum

Announcement

Collapse
No announcement yet.

Converting std::string to TCHAR and vice verse

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Converting std::string to TCHAR and vice verse

    I struggled with this A LOT. Xeno123 tried to help me with his mexican boost casts and i tried and tried but finally found this way and ended up using it, i really hope this will be useful for other people too who have similiar problems, at least it fixed my problem :p

    Code:
    typedef std::basic_string<TCHAR> tstring;  
     
    TCHAR* StringToTCHAR(string& s)
    {
      tstring tstr;
      const char* all = s.c_str();
      int len = 1 + strlen(all);
      wchar_t* t = new wchar_t[len]; 
      if (NULL == t) throw std::bad_alloc();
      mbstowcs(t, all, len);
      return (TCHAR*)t;
    }
     
    std::string TCHARToString(const TCHAR* ptsz)
    {
         int len = wcslen((wchar_t*)ptsz);
         char* psz = new char[2*len + 1];
         wcstombs(psz, (wchar_t*)ptsz, 2*len + 1);
         std::string s = psz;
         delete [] psz;
         return s;
    }
    Original link: How to convert std::string to TCHAR*
    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

    #2
    DUDE. Don't use the fucking microsoft shit. mbtowcs? I am so fucking disappoint.

    Comment

    Working...
    X