Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Convert from TCHAR* to char*

Convert from TCHAR* to char*

Scheduled Pinned Locked Moved C / C++ / MFC
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    x87Bliss
    wrote on last edited by
    #1

    I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:

    CString szVal = _T("Some User Input String"); // i.e. not always the same value
    char* Buf;
    size_t BufLen;
    #ifdef UNICODE
    wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
    szBuf = new char[szBufLen];
    wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
    #else
    szBuf = new char[szVal.GetLength() + 1];
    strcpy(szBuf, szVal);
    #endif
    // more code
    delete [] szBuf;

    The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.

    L J 2 Replies Last reply
    0
    • X x87Bliss

      I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:

      CString szVal = _T("Some User Input String"); // i.e. not always the same value
      char* Buf;
      size_t BufLen;
      #ifdef UNICODE
      wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
      szBuf = new char[szBufLen];
      wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
      #else
      szBuf = new char[szVal.GetLength() + 1];
      strcpy(szBuf, szVal);
      #endif
      // more code
      delete [] szBuf;

      The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      x87Bliss wrote:

      but I was hoping there was already a function defined.

      Well while you were hoping, I was searching.[^] :rolleyes:

      led mike

      X 1 Reply Last reply
      0
      • X x87Bliss

        I am writing an application that communicates with an external device. I need to copy a CString (TCHAR*) to a multibyte string buffer (char*) - the catch is I need the code to be able to compile with UNICODE on or off. Basically I need a function that can copy either a WCHAR* or char* to a char*. Currently I have:

        CString szVal = _T("Some User Input String"); // i.e. not always the same value
        char* Buf;
        size_t BufLen;
        #ifdef UNICODE
        wcstombs_s(&szBufLen, NULL, NULL, szVal, _TRUNCATE); // get the req'd length including NULL
        szBuf = new char[szBufLen];
        wcstombs_s(&szBufLen, szBuf, szBufLen, szVal, _TRUNCATE); // convert and copy
        #else
        szBuf = new char[szVal.GetLength() + 1];
        strcpy(szBuf, szVal);
        #endif
        // more code
        delete [] szBuf;

        The above works, but I would prefer not having to use the preprocessor directives if posssible. The function _tcscpy will not convert from WCHAR to char when needed. I know I can easily write my own overloaded function, but I was hoping there was already a function defined.

        J Offline
        J Offline
        Jijo Raj
        wrote on last edited by
        #3

        What about T2A() macro? Refer it here - http://msdn.microsoft.com/en-us/library/87zae4a3(VS.80).aspx[^] Regards, Jijo.

        _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

        1 Reply Last reply
        0
        • L led mike

          x87Bliss wrote:

          but I was hoping there was already a function defined.

          Well while you were hoping, I was searching.[^] :rolleyes:

          led mike

          X Offline
          X Offline
          x87Bliss
          wrote on last edited by
          #4

          Thanks. Sorry, I apparently am not good at searching; I could only find the functions for WCHAR and char, not TCHAR. Those macros are perfect.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups