Switching from Multi-Byte to Unicode character sets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?amM=?=

    Switching from Multi-Byte to Unicode character sets

    Hello,
    This compiles OK using Multi-Byte character set,
    but when I switch to Unicode I get an error.
    char reply[256] = _T("olleh");

    I know this will fix the error with the Unicode compile,
    wchar_t reply[256] = _T("olleh");
    but this causes many other conversion problems in the program.

    Is there some other conversion macro that I could use instead of _T(),
    instead of having to change from char to a wchar_t?

    Thanks,
    -JC


  • Jochen Kalmbach [MVP]

    #2
    Re: Switching from Multi-Byte to Unicode character sets

    Hi jc!
    char reply[256] = _T("olleh");
    You should get used to TCHAR:

    TCHAR reply[256] = _T("olleh");

    --
    Greetings
    Jochen

    My blog about Win32 and .NET

    Comment

    • David Wilkinson

      #3
      Re: Switching from Multi-Byte to Unicode character sets

      jc wrote:
      Hello,
      This compiles OK using Multi-Byte character set,
      but when I switch to Unicode I get an error.
      char reply[256] = _T("olleh");
      >
      I know this will fix the error with the Unicode compile,
      wchar_t reply[256] = _T("olleh");
      but this causes many other conversion problems in the program.
      >
      Is there some other conversion macro that I could use instead of _T(),
      instead of having to change from char to a wchar_t?
      JC:

      You have to write

      TCHAR reply[256] = _T("olleh");

      if you still want to compile for multi-byte,

      or

      wchar_t reply[256] = L"olleh";

      if you only need to compile for Unicode.

      If these cause problems for your code, these problems need to be fixed.

      You would not be having this problem if you had used TCHAR, _T("") etc originally.

      --
      David Wilkinson
      Visual C++ MVP

      Comment

      • Giovanni Dicanio

        #4
        Re: Switching from Multi-Byte to Unicode character sets


        "jc" <jc@discussions .microsoft.comh a scritto nel messaggio
        news:08230DC5-0A9E-468F-AE81-0054ED8ED0B4@mi crosoft.com...
        Hello,
        This compiles OK using Multi-Byte character set,
        but when I switch to Unicode I get an error.
        char reply[256] = _T("olleh");
        Because when you switch to Unicode, _T("...") macro expands to L"...", so
        your code becomes:

        char reply[ 256 ] = L"olleh"

        which is wrong (because the L"olleh" is a wchar_t string, not a char
        string).

        I know this will fix the error with the Unicode compile,
        wchar_t reply[256] = _T("olleh");
        but this causes many other conversion problems in the program.
        The correct code should be:

        TCHAR reply[ 256 ] = _T("olleh");

        TCHAR will expands to wchar_t in Unicode builds, and to char in ANSI/MBCS
        builds.

        BTW: why don't you use CString instead of raw TCHAR arrays?

        Is there some other conversion macro that I could use instead of _T(),
        instead of having to change from char to a wchar_t?
        If you want to convert from generic TCHAR to char, you may want to use ATL
        helper classes, like CT2A:

        CT2A reply( _T("olleh") );

        Both in Unicode and in ANSI/MBCS builds, your 'reply' variable is a RAII
        buffer made by char's.

        More information here:



        HTH,
        Giovanni


        Comment

        • Mark Salsbery [MVP]

          #5
          Re: Switching from Multi-Byte to Unicode character sets

          In addition to the other replies, here's some more info on using the generic
          text mappings...

          Generic-Text Mappings in Tchar.h



          Mark

          --
          Mark Salsbery
          Microsoft MVP - Visual C++


          Comment

          • =?Utf-8?B?amM=?=

            #6
            Re: Switching from Multi-Byte to Unicode character sets

            "Jochen Kalmbach [MVP]" wrote:
            Hi jc!
            >
            char reply[256] = _T("olleh");
            >
            You should get used to TCHAR:
            >
            TCHAR reply[256] = _T("olleh");
            >
            --
            Greetings
            Jochen
            >
            My blog about Win32 and .NET

            >
            Hi Jochen,

            Thanks for replying to my post. Yes, the TCHAR worked, and the other
            conversions throughout the program were not that painful !!
            -jc

            Comment

            Working...