我有一个包含0x0的字符串问题,能否有人帮助我?
如果我打印出 a 和 b 的内容,它们都显示为“test”。
我的问题是,为什么
摘要: 感谢众多回复。我现在认为问题已经清楚了。来自Crypto++库的外部
string a = funxx(); // funxx is a external func from Crypto++ libary
string b = "test"
如果我打印出 a 和 b 的内容,它们都显示为“test”。
a.size() is: 5
b.size() is: 4
a.length() is: 5
b.length() is: 4
for( int i = 0; i < a.size(); i++ ) {
cout << "0x" << hex << (0xFF & static_cast<byte>(a[i])) << " ";
}
print out: 0x74 0x65 0x73 0x74 0x0
for( int i = 0; i < b.size(); i++ ) {
cout << "0x" << hex << (0xFF & static_cast<byte>(b[i])) << " ";
}
print out: 0x74 0x65 0x73 0x74
我的问题是,为什么
a.length()
和a.size()
返回5
而不是4
,如何使a
和b
相等?摘要: 感谢众多回复。我现在认为问题已经清楚了。来自Crypto++库的外部
func
在字符串中返回了一个额外的空字符,并且在字符串中单独跟踪长度。
我需要做的是std::string(a.c_str())
std::string
可以包含空字符,因为长度是单独跟踪的,这并不会有任何问题。 - chrisstd::string(a.c_str())
- Bryan Chen