问题描述
如何在Visual C中获取多字节字符字符串的字节大小?有功能吗,还是我必须自己计算字符数?
How do I get the byte size of a multibyte-character string in Visual C? Is there a function or do I have to count the characters myself?
或更笼统地说,如何获得TCHAR字符串的正确字节大小?
Or, more general, how do I get the right byte size of a TCHAR string?
解决方案:
_tcslen(_T("TCHAR string")) * sizeof(TCHAR)
我只在谈论以null终止的字符串.
I was talking about null-terminated strings only.
推荐答案
根据MSDN ,当定义_MBCS
时,_tcslen
对应于strlen
. strlen
将返回字符串中的 bytes 数.如果使用与_mbslen
相对应的_tcsclen
,则返回多字节字符的数量.
According to MSDN, _tcslen
corresponds to strlen
when _MBCS
is defined. strlen
will return the number of bytes in the string. If you use _tcsclen
that corresponds to _mbslen
which returns the number of multibyte characters.
此外,多字节字符串(AFAIK)不包含嵌入的null,不.
Also, multibyte strings do not (AFAIK) contain embedded nulls, no.
我首先会质疑使用多字节编码,但是...除非您支持旧版应用程序,否则没有理由选择多字节而不是Unicode.
I would question the use of a multibyte encoding in the first place, though... unless you're supporting a legacy app, there's no reason to choose multibyte over Unicode.
这篇关于如何获取多字节字符串的字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!