簡單的C++程序,它爲字符串添加一個char字節。結果長度在輸出中是錯誤的。char concat to string returns wrong length
#include <iostream>
#include <string>
int main(){
char x = 0x01;
std::string test;
test = x+"test";
std::cout << "length: " << test.length() << std::endl;
std::cout << "test: " << test << std::endl;
return 0;
}
輸出:
length: 3
test: est
我前面加上一個類型字節的字符串,因爲我要通過一個插座,另一端可以發送這個數據有一個工廠,需要知道要創建的對象的類型。
請參閱相關的問題:http://stackoverflow.com/questions/28957950/why-in-the-code -4561-output-is-56 – juanchopanza