按照MSDN的说吗,在选用MBCS多字节字符串编码时,该方法会得到正确的字节数。此时没有问题。

      For multibyte character sets (MBCS), GetLength counts each 8-bit character; that is, a lead and trail byte in one multibyte character are counted as two bytes.

      但是在Unicode编码下,一旦出现中文字符,该方法就会少统计。

      我试用最多的解决方法是:

      CString str("abc我");

      DWORD le0 = str.GetLength(); // 返回4,不是想要的字节数

      // 这样处理就对了。先用CStringA类转化成多字节字符串。

      le0 = CStringA(str).GetLength();

 

另外,也有人这样用,也可以。比上面效率高。

       DWORD le0 = str.GetLength() * sizeof(TCHAR);

// 这种用法在MBCS环境下可以省略。在Unicode下,所有字符(包括ascii及中文字符), 每个字符都被定义为WHAR, 即双字节Unicode字符。该方法也正确。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2022-12-23
  • 2021-12-19
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-05-18
  • 2021-10-29
  • 2022-12-23
  • 2021-12-21
  • 2021-10-16
  • 2022-12-23
相关资源
相似解决方案