长度规则如下:一个中文长度为2,英文为1:
经过Unicode下项目测试OK,关键代码段如下:
int get_lenth(CString strSrc)
{
// 获取正确的长度
USES_CONVERSION;
int nlen = -1;
char* pch = W2A( strSrc);
int count{}; //定义返回的长度
for (int i = 0; pch[i]; i++)
{
if (pch[i] < 0)
{
i++; //只要小于0,说明是汉字,占用两个字节,后一个字节跳过,不统计
count += 2;
}
else
count++;
}
return count;
}
比如,Cstring str= _T("中文123");
int nLen = get_length(str);
nLen的最终结果会是 7,而不是 GetLength()得到的5.