为什么std :: string(“ \x00”)报告的长度为0? [英] Why does std::string("\x00") report length of 0?
本文介绍了为什么std :: string(“ \x00”)报告的长度为0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要对字符串进行编码的函数,该函数必须能够接受0x00作为有效的字节。我的程序需要检查字符串的长度,但是如果我将 \x00
传递给 std :: string
length()
方法返回0。
I have a function which needs to encode strings, which needs to be able to accept 0x00 as a valid 'byte'. My program needs to check the length of the string, however if I pass in "\x00"
to std::string
the length()
method returns 0.
即使字符串是一个空字符?
How can I get the actual length even if the string is a single null character?
推荐答案
您传入的是空字符串。使用 std :: string(1,'\0')
代替。
You're passing in an empty string. Use std::string(1, '\0')
instead.
或 std :: string {'\0'}
(感谢@ zett42)
Or std::string{ '\0' }
(thanks, @zett42)
这篇关于为什么std :: string(“ \x00”)报告的长度为0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文