Is there any need for me to use wstring in the following case

Posted by Yan Cheng CHEOK on Stack Overflow See other posts from Stack Overflow or by Yan Cheng CHEOK
Published on 2010-03-16T09:34:46Z Indexed on 2010/03/16 9:36 UTC
Read the original article Hit count: 207

Filed under:
|
|

Currently, I am developing an app for a China customer. China customer are mostly switch to GB2312 language in their OS encoding. I need to write a text file, which will be encoded using GB2312.

  1. I use std::ofstream file
  2. I compile my application under MBCS mode, not unicode.
  3. I use the following code, to convert CString to std::string, and write it to file using ofstream

std::string Utils::ToString(CString& cString) {
    /* Will not work correctly, if we are compiled under unicode mode. */
    return (LPCTSTR)cString;
}

To my surprise. It just works. I thought I need to at least make use of wstring. I try to do some investigation.

Here is the MBCS.txt generated.

alt text

  1. I try to print a single character named ? (its value is 0xBDC5)
  2. When I use CString to carry this character, its length is 2.
  3. When I use Utils::ToString to perform conversion to std::string, the returned string length is 2.
  4. I write to file using std::ofstream

My question is :

  1. When I exam MBCS.txt using a hex editor, the value is displayed as BD (LSB) and C5 (MSB). But I am using little endian machine. Isn't hex editor should show me C5 (LSB) and BD (MSB)? I check from wikipedia. GB2312 seems doesn't specific endianness.
  2. It seems that using std::string + CString just work fine for my case. May I know in what case, the above methodology will not work? and when I should start to use wstring?

© Stack Overflow or respective owner

Related posts about unicode

Related posts about mfc