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. Using TCHAR and char array

Using TCHAR and char array

Scheduled Pinned Locked Moved C / C++ / MFC
databasedata-structuresdebuggingjsonhelp
12 Posts 4 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.
  • C CPallini

    You should zero-terminate the string, I suppose, i.e.

    array[6] = 0
    array[7] = 0

    :) Nevermind, I was a it hasty. :-O

    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
    [My articles]

    modified on Thursday, October 8, 2009 1:55 PM

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #3

    Not quite, he's sending a char* which gets filled with TCHARs. It's the old Unicode <--> ASCII confusion again! Do people not read the manuals anymore?

    D C 2 Replies Last reply
    0
    • C CPallini

      You should zero-terminate the string, I suppose, i.e.

      array[6] = 0
      array[7] = 0

      :) Nevermind, I was a it hasty. :-O

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      modified on Thursday, October 8, 2009 1:55 PM

      D Offline
      D Offline
      dipuks
      wrote on last edited by
      #4

      When i try to write the "array" string into a file, all that it writes is "A", and its ignoring the remaining array indexes. Can this be solved by null terminating? How to do that?

      L 1 Reply Last reply
      0
      • L Lost User

        Not quite, he's sending a char* which gets filled with TCHARs. It's the old Unicode <--> ASCII confusion again! Do people not read the manuals anymore?

        D Offline
        D Offline
        dipuks
        wrote on last edited by
        #5

        Richard, Can you tell me how to fix this issue?

        1 Reply Last reply
        0
        • D dipuks

          When i try to write the "array" string into a file, all that it writes is "A", and its ignoring the remaining array indexes. Can this be solved by null terminating? How to do that?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          dipuks wrote:

          Can this be solved by null terminating?

          The short answer is, no! You need to understand the difference between TCHAR (Unicode, 16 bit) and ASCII (8-bit) characters. You cannot store a TCHAR string into a char[] array and expect the program to make sense of it. From the small piece of code you posted you just need to define your array as TCHAR[] and use the appropriate function calls to process the text. Take a look here in MSDN[^] for some more information.

          R 1 Reply Last reply
          0
          • D dipuks

            Hi I have an API function call that asks to send a TCHAR* as an argument and it will return a string on the TCHAR* Instead of sending a TCHAR* i send it as a char array, like this char array[12]; Now when i debug and look at it, i see array[0] = A array[1] = 0 array[2] = B array[3] = 0 array[4] = C array[5] = 0 So if am expecting an output string of "ABC". What am seeing is, everyother index in the array is set to "0". Why's this happening? Is it a problem with using char array in place of TCHAR? How to use the TCHAR?

            R Offline
            R Offline
            Rajesh R Subramanian
            wrote on last edited by
            #7

            Hi, You are doing an Unicode build (have defined _UNICODE instead of _MBCS) and therefore TCHAR behaves like wchar_t (double byte character). The API is obviously returning an Unicode string to you, and you have accepted it into an ANSI style string. The Unicode version of the API was called, because you are doing an Unicode build. To solve this, you could do an MBCS build. However, I strongly recommend the usage of TCHAR instead of char and use neutral string manipulation functions and APIs - for example, _tcscpy instead of strcpy. _tcscpy will behave like strcpy when you do an MBCS build and will behave like wcscpy when you do an Unicode build. This way, you could do an Unicode build and an MBCS build without making changes in the source code. Take a look at the "Windows Data types" section of this article[^]. I would also recommend this two part article to you: The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^] The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^]

            It is a crappy thing, but it's life -^ Carlo Pallini

            D 1 Reply Last reply
            0
            • L Lost User

              dipuks wrote:

              Can this be solved by null terminating?

              The short answer is, no! You need to understand the difference between TCHAR (Unicode, 16 bit) and ASCII (8-bit) characters. You cannot store a TCHAR string into a char[] array and expect the program to make sense of it. From the small piece of code you posted you just need to define your array as TCHAR[] and use the appropriate function calls to process the text. Take a look here in MSDN[^] for some more information.

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #8

              Richard MacCutchan wrote:

              You need to understand the difference between TCHAR (Unicode, 16 bit) and ASCII (8-bit) characters.

              TCHAR is not an Unicode character. TCHAR is defined either as an Unicode character (wchar_t) or an ASCII character (char), depending on the build.

              It is a crappy thing, but it's life -^ Carlo Pallini

              L 1 Reply Last reply
              0
              • R Rajesh R Subramanian

                Richard MacCutchan wrote:

                You need to understand the difference between TCHAR (Unicode, 16 bit) and ASCII (8-bit) characters.

                TCHAR is not an Unicode character. TCHAR is defined either as an Unicode character (wchar_t) or an ASCII character (char), depending on the build.

                It is a crappy thing, but it's life -^ Carlo Pallini

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Rajesh R Subramanian wrote:

                TCHAR is defined either as

                Quite correct. However, I was trying to keep it simple(r) as he was obviously putting Unicode characters into the array.

                1 Reply Last reply
                0
                • L Lost User

                  Not quite, he's sending a char* which gets filled with TCHARs. It's the old Unicode <--> ASCII confusion again! Do people not read the manuals anymore?

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #10

                  You're right, of course. Too much drugz, today... :rolleyes: :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  L 1 Reply Last reply
                  0
                  • R Rajesh R Subramanian

                    Hi, You are doing an Unicode build (have defined _UNICODE instead of _MBCS) and therefore TCHAR behaves like wchar_t (double byte character). The API is obviously returning an Unicode string to you, and you have accepted it into an ANSI style string. The Unicode version of the API was called, because you are doing an Unicode build. To solve this, you could do an MBCS build. However, I strongly recommend the usage of TCHAR instead of char and use neutral string manipulation functions and APIs - for example, _tcscpy instead of strcpy. _tcscpy will behave like strcpy when you do an MBCS build and will behave like wcscpy when you do an Unicode build. This way, you could do an Unicode build and an MBCS build without making changes in the source code. Take a look at the "Windows Data types" section of this article[^]. I would also recommend this two part article to you: The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^] The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^]

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    D Offline
                    D Offline
                    dipuks
                    wrote on last edited by
                    #11

                    Thanks guys

                    1 Reply Last reply
                    0
                    • C CPallini

                      You're right, of course. Too much drugz, today... :rolleyes: :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      CPallini wrote:

                      Too much drugz, today

                      Luckeee! :cool:

                      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