如何获取多字节字符串的字节大小 [英] How to get byte size of multibyte string

查看:214
本文介绍了如何获取多字节字符串的字节大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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.

推荐答案

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.

这篇关于如何获取多字节字符串的字节大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆