如何计算CString所占用的字节数?

时间:2023-01-09 23:24:19
如题。。。要兼容UNICODE和非UNICODE。。。

例如:
CString strTemp = TEXT("ABCD")
int iLength = YourFunction(strTemp);
//UNICODE下iLength应为8
//非UNICODE下iLength应为4

16 个解决方案

#1


str.GetLength();

#2


getbuffer
sizeof

#3


strTem.GetLength();

#4


GetLength总是4.....

#5


引用 2 楼 mayudong1 的回复:
getbuffer
 sizeof

刚没看清楚,我没试过GetLength在Unicode下得到的长度。
但是这个肯定是对的。
用完以后ReleaseBuffer

#6


int iLength = sizeof(strTemp.GetBuffer(strTemp.GetLength()));//也是4。。。

#7


把GetBuffer的参数换成0呢?

#8


引用 2 楼 mayudong1 的回复:
getbuffer
 sizeof
这个可以有

#9


int iLength = sizeof(strTemp.GetBuffer(0));//仍然是4。。。
//55555应该是8啊。。。。我要8。。。。

#10


(strTemp.GetLength())*sizeof(CHAR)

#11


友情UP。

CString的成员函数可以实现。UNICODE和非ANSIC根据平台来定。

#12


(strTemp.GetLength())*sizeof(TCHAR)

#13


引用 12 楼 jingzhongrong 的回复:
(strTemp.GetLength())*sizeof(TCHAR)

正解。。。

引用 10 楼 xueer8835 的回复:
(strTemp.GetLength())*sizeof(CHAR)

差一点。。。

#14



//例子举的很不好。。。 应该是。。。
CString strTemp = "ABCDE";
//才能发现sizeof的问题。。。

#15


引用 12 楼 jingzhongrong 的回复:
(strTemp.GetLength())*sizeof(TCHAR)

俺今天怎么总犯小错误,5555555555,回家

#16


(strTemp.GetLength())*sizeof(wchar_t);

#1


str.GetLength();

#2


getbuffer
sizeof

#3


strTem.GetLength();

#4


GetLength总是4.....

#5


引用 2 楼 mayudong1 的回复:
getbuffer
 sizeof

刚没看清楚,我没试过GetLength在Unicode下得到的长度。
但是这个肯定是对的。
用完以后ReleaseBuffer

#6


int iLength = sizeof(strTemp.GetBuffer(strTemp.GetLength()));//也是4。。。

#7


把GetBuffer的参数换成0呢?

#8


引用 2 楼 mayudong1 的回复:
getbuffer
 sizeof
这个可以有

#9


int iLength = sizeof(strTemp.GetBuffer(0));//仍然是4。。。
//55555应该是8啊。。。。我要8。。。。

#10


(strTemp.GetLength())*sizeof(CHAR)

#11


友情UP。

CString的成员函数可以实现。UNICODE和非ANSIC根据平台来定。

#12


(strTemp.GetLength())*sizeof(TCHAR)

#13


引用 12 楼 jingzhongrong 的回复:
(strTemp.GetLength())*sizeof(TCHAR)

正解。。。

引用 10 楼 xueer8835 的回复:
(strTemp.GetLength())*sizeof(CHAR)

差一点。。。

#14



//例子举的很不好。。。 应该是。。。
CString strTemp = "ABCDE";
//才能发现sizeof的问题。。。

#15


引用 12 楼 jingzhongrong 的回复:
(strTemp.GetLength())*sizeof(TCHAR)

俺今天怎么总犯小错误,5555555555,回家

#16


(strTemp.GetLength())*sizeof(wchar_t);