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. ATL / WTL / STL
  4. need help selecting a string type

need help selecting a string type

Scheduled Pinned Locked Moved ATL / WTL / STL
c++csharpvisual-studiocomdesign
11 Posts 3 Posters 12 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.
  • B Offline
    B Offline
    bkelly13
    wrote on last edited by
    #1

    Windows 7 and XP, Visual Studio 2008, 2010, C++, MFC, Windows 32 application. I am getting frustrated with the various types of strings. With all the articles I find and often opposing points of view, I wish to limit my options and seek your opinion. I write telemetry code that must run quite fast and move much data. The strings I deal with are mostly to log data so I can see how the code works. The end product will be a windows application that really needs no user interface. However, during development, and because a vendor uses MFC and C++, I wish to develop in that environment. I have no desire to explicitly exclude other operating systems, but I really do not expect to use anything other than Windows XP and Windows 7 any times soon. Which option is best: WCHAR, wchar_t, CString, char[], or something else? edit: I forgot that WCHAR is just a typedef of wchar_t. Which does not seem very bright. Would it be wise to drop all use of WHAR and replace with wchar_t?

    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

    L 1 Reply Last reply
    0
    • B bkelly13

      Windows 7 and XP, Visual Studio 2008, 2010, C++, MFC, Windows 32 application. I am getting frustrated with the various types of strings. With all the articles I find and often opposing points of view, I wish to limit my options and seek your opinion. I write telemetry code that must run quite fast and move much data. The strings I deal with are mostly to log data so I can see how the code works. The end product will be a windows application that really needs no user interface. However, during development, and because a vendor uses MFC and C++, I wish to develop in that environment. I have no desire to explicitly exclude other operating systems, but I really do not expect to use anything other than Windows XP and Windows 7 any times soon. Which option is best: WCHAR, wchar_t, CString, char[], or something else? edit: I forgot that WCHAR is just a typedef of wchar_t. Which does not seem very bright. Would it be wise to drop all use of WHAR and replace with wchar_t?

      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

      IT depends on what you are trying to do, and what may change in the future. If you want a simple self-managing string type and you have MFC, then use CString. If you want speed and more control of the string data then use character arrays. If you choose the latter course then your decision will be based on the type of data that you are processing: if it is ASCII data then use char arrays, if Unicode use WCHAR arrays.

      B 1 Reply Last reply
      0
      • L Lost User

        IT depends on what you are trying to do, and what may change in the future. If you want a simple self-managing string type and you have MFC, then use CString. If you want speed and more control of the string data then use character arrays. If you choose the latter course then your decision will be based on the type of data that you are processing: if it is ASCII data then use char arrays, if Unicode use WCHAR arrays.

        B Offline
        B Offline
        bkelly13
        wrote on last edited by
        #3

        The fundamental purpose is to capture telemetry data, all numbers, reformat them, and send them to another computer. HOWEVER: as often needed, I need insight into the application. Keeping this short: An MFC is used and sometimes an AFXMsgBox window is needed to inform the user of errors detected. There is also a log file. It needs to be read to see how the program operated. Does that help narrow the field? Edit: I read that I really should switch to Unicode, so I did and started using WCHAR. Now I discovered that it is just a retype of wchar_t, and further that it is Microsoft unique. I do not expect to write code that can be dropped into a Unix machine and compiled, but would like to be reasonably compatible. So what the heck should be used?

        Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

        L 1 Reply Last reply
        0
        • B bkelly13

          The fundamental purpose is to capture telemetry data, all numbers, reformat them, and send them to another computer. HOWEVER: as often needed, I need insight into the application. Keeping this short: An MFC is used and sometimes an AFXMsgBox window is needed to inform the user of errors detected. There is also a log file. It needs to be read to see how the program operated. Does that help narrow the field? Edit: I read that I really should switch to Unicode, so I did and started using WCHAR. Now I discovered that it is just a retype of wchar_t, and further that it is Microsoft unique. I do not expect to write code that can be dropped into a Unix machine and compiled, but would like to be reasonably compatible. So what the heck should be used?

          Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

          bkelly13 wrote:

          Does that help narrow the field?

          Not really, since all of those issues may be handled in ASCII or Unicode without any problems. You omitted to explain the format of the input data: numbers as in characters (ASCII or Unicode), or numbers as in binary values? And, if the latter what do you convert them into (if anything). If you are using Unicode for all your text then stick with WCHAR, since it's a macro and can be defined to equate to any native unicode type on other platforms.

          B 1 Reply Last reply
          0
          • L Lost User

            bkelly13 wrote:

            Does that help narrow the field?

            Not really, since all of those issues may be handled in ASCII or Unicode without any problems. You omitted to explain the format of the input data: numbers as in characters (ASCII or Unicode), or numbers as in binary values? And, if the latter what do you convert them into (if anything). If you are using Unicode for all your text then stick with WCHAR, since it's a macro and can be defined to equate to any native unicode type on other platforms.

            B Offline
            B Offline
            bkelly13
            wrote on last edited by
            #5

            The data is received as binary values and is signed, unsigned, with sizes from 1 bit per parameter to 32 bits. Some value are floating point number. All binary format, no text in any form. My application picks them out of the stream, shifts them into the correct position, applies scale and offset, assigns identifying tag numbers, and TCP the data out to a display device. The text is used to create a configuration file for each of the different telemetry types. That is done with VBA for Excel that I write. Log files that show performance are in a text format that are read with Microsoft Wordpad. The startup configuration work and the performance monitoring use text. All the primary operations are shuffling binary data around and do not use any text. Your comment that WCHAR can be redefined to something else for another device was a revelation. Simple and obvious and completely missed. It makes a rather big difference. Thank you for the idea. I was thinking of std::string. Do you think that would be a valid choice? Because of your comment it is not as likely, but I would like to hear you thoughts on that anyway.

            Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

            L 1 Reply Last reply
            0
            • B bkelly13

              The data is received as binary values and is signed, unsigned, with sizes from 1 bit per parameter to 32 bits. Some value are floating point number. All binary format, no text in any form. My application picks them out of the stream, shifts them into the correct position, applies scale and offset, assigns identifying tag numbers, and TCP the data out to a display device. The text is used to create a configuration file for each of the different telemetry types. That is done with VBA for Excel that I write. Log files that show performance are in a text format that are read with Microsoft Wordpad. The startup configuration work and the performance monitoring use text. All the primary operations are shuffling binary data around and do not use any text. Your comment that WCHAR can be redefined to something else for another device was a revelation. Simple and obvious and completely missed. It makes a rather big difference. Thank you for the idea. I was thinking of std::string. Do you think that would be a valid choice? Because of your comment it is not as likely, but I would like to hear you thoughts on that anyway.

              Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

              bkelly13 wrote:

              I was thinking of std::string

              That would also be a valid choice, although be aware that std::string is ASCII only, for Unicode you need to use std::wstring. I tend to use a typedef to define my own type which will be ASCII or Unicode, dependiing on the project settings. Something like:

              #if defined(UNICODE)
              typedef std::wstring STRING;
              #else
              typedef std::string STRING;
              #endif

              Then I just use STRING everywhere in the rest of the code, and the compiler sorts it out for me. From your comments above and the description of what your code is required to do, it seems that the choice is far less important than your first message implied.

              B T 2 Replies Last reply
              0
              • L Lost User

                bkelly13 wrote:

                I was thinking of std::string

                That would also be a valid choice, although be aware that std::string is ASCII only, for Unicode you need to use std::wstring. I tend to use a typedef to define my own type which will be ASCII or Unicode, dependiing on the project settings. Something like:

                #if defined(UNICODE)
                typedef std::wstring STRING;
                #else
                typedef std::string STRING;
                #endif

                Then I just use STRING everywhere in the rest of the code, and the compiler sorts it out for me. From your comments above and the description of what your code is required to do, it seems that the choice is far less important than your first message implied.

                B Offline
                B Offline
                bkelly13
                wrote on last edited by
                #7

                Re: well, yes and no. It is less important on this particular project. It is more important in that as I become used to one type or the other I will tend to use it on subsequent projects. Not very many people/places are heavy into telemetry and some of those are unix/linix shops. WCHAR is fine, but I am thinking that std::wstring might be the better option. The TCP/IP will make it difficult to port, but I can do little about that. (Well, not true, but out of scope.) Thank you for taking the time to reply.

                Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                L 1 Reply Last reply
                0
                • B bkelly13

                  Re: well, yes and no. It is less important on this particular project. It is more important in that as I become used to one type or the other I will tend to use it on subsequent projects. Not very many people/places are heavy into telemetry and some of those are unix/linix shops. WCHAR is fine, but I am thinking that std::wstring might be the better option. The TCP/IP will make it difficult to port, but I can do little about that. (Well, not true, but out of scope.) Thank you for taking the time to reply.

                  Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

                  bkelly13 wrote:

                  The TCP/IP will make it difficult to port

                  If you stick to the basic TCP/IP functions (connect, listen, send, recv) it will port with no problems.

                  B 1 Reply Last reply
                  0
                  • L Lost User

                    bkelly13 wrote:

                    The TCP/IP will make it difficult to port

                    If you stick to the basic TCP/IP functions (connect, listen, send, recv) it will port with no problems.

                    B Offline
                    B Offline
                    bkelly13
                    wrote on last edited by
                    #9

                    I am writing this to use non-blocking and overlapped with events to signify I/O completion. The WSA* calls are used. Does that reduce portability?

                    Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

                    L 1 Reply Last reply
                    0
                    • B bkelly13

                      I am writing this to use non-blocking and overlapped with events to signify I/O completion. The WSA* calls are used. Does that reduce portability?

                      Thank you for your time If you work with telemetry, please check this bulletin board: www.irigbb.com

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

                      Yes, WSA functions are all Windows sockets, so not directly portable. Although you could always write some code of your own to cater for that if necessary.

                      1 Reply Last reply
                      0
                      • L Lost User

                        bkelly13 wrote:

                        I was thinking of std::string

                        That would also be a valid choice, although be aware that std::string is ASCII only, for Unicode you need to use std::wstring. I tend to use a typedef to define my own type which will be ASCII or Unicode, dependiing on the project settings. Something like:

                        #if defined(UNICODE)
                        typedef std::wstring STRING;
                        #else
                        typedef std::string STRING;
                        #endif

                        Then I just use STRING everywhere in the rest of the code, and the compiler sorts it out for me. From your comments above and the description of what your code is required to do, it seems that the choice is far less important than your first message implied.

                        T Offline
                        T Offline
                        Theo Buys
                        wrote on last edited by
                        #11

                        Sometimes you need both. For UTF-8 unicode I use std::string For UTF-16 unicode I use std::wstring

                        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