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. TCHAR Conversions from char*

TCHAR Conversions from char*

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structureshelptutorial
7 Posts 6 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.
  • N Offline
    N Offline
    Not Knuth
    wrote on last edited by
    #1

    I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!

    N T J 3 Replies Last reply
    0
    • N Not Knuth

      I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      there is an api for the same MultiByteToWideChar()....

      nave

      1 Reply Last reply
      0
      • N Not Knuth

        I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        Not Knuth wrote:

        How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else?

        wcstombs

        "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

        cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

        1 Reply Last reply
        0
        • N Not Knuth

          I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!

          J Offline
          J Offline
          John Saunders
          wrote on last edited by
          #4

          Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.

          P N 2 Replies Last reply
          0
          • J John Saunders

            Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.

            P Offline
            P Offline
            PJ Arends
            wrote on last edited by
            #5

            John Saunders wrote:

            Place the macro USES_CONVERSION at the beginning of your function

            John Saunders wrote:

            Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.

            If you use the newer ATL7 conversion macros then you do not have to use USES_CONVERSION and you can safely use the conversion macros inside a loop. http://msdn2.microsoft.com/en-us/library/87zae4a3.aspx[^]


            You may be right
            I may be crazy
            -- Billy Joel --

            Within you lies the power for good, use it!!!

            1 Reply Last reply
            0
            • J John Saunders

              Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.

              N Offline
              N Offline
              Not Knuth
              wrote on last edited by
              #6

              Thanks, this works fine. But I am wondering how the macros work. I followed the code and see that it ultimately ends up at defs for MultiByteToWideChar and others. But of course the code is not shown. So I wonder if this is implemented as just a big lookup table or if there is some better way to approach this. Obviously this question has become somewhat academic at this point. My initial problem is resolved. But I am just thinking about how I would have implemented this and I don't see a better way than using a big lookup. That seems too clumsy.

              Z 1 Reply Last reply
              0
              • N Not Knuth

                Thanks, this works fine. But I am wondering how the macros work. I followed the code and see that it ultimately ends up at defs for MultiByteToWideChar and others. But of course the code is not shown. So I wonder if this is implemented as just a big lookup table or if there is some better way to approach this. Obviously this question has become somewhat academic at this point. My initial problem is resolved. But I am just thinking about how I would have implemented this and I don't see a better way than using a big lookup. That seems too clumsy.

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                An alternative to using the ATL macros is to use the _bstr_t class. It has constructors that take both ASCII and WIDE character sets, and have overloaded operators for both char* and wchar_t*. It will end up calling the same functions in the end (the MultiByteToWide, etc.) but it easier to read and debug since it isn't a macro.

                If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                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