need help selecting a string type
-
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
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 usechar
arrays, if Unicode useWCHAR
arrays. -
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 usechar
arrays, if Unicode useWCHAR
arrays.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
-
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
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.
-
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.
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
-
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
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 usestd::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;
#endifThen 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. -
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 usestd::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;
#endifThen 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.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
-
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
-
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.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
-
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
-
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 usestd::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;
#endifThen 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.