我无法将常量字符转换为字节。我正在阅读使用ifstream的文件,它给我的内容作为字符串,然后我使用c_str()将字符串转换为常量字符。然后尝试将其插入到字节数组以用于数据包发送目的。我是新来的C + +不能理解我必须如何将字符转换为字节,需要你的帮助球员。这里是我的一段代码,请给我一些建议从`const char *'转换为`byte'
byte buf[42];
const char* fname = path.c_str();
ifstream inFile;
inFile.open(fname);//open the input file
stringstream strStream;
strStream << inFile.rdbuf();//read the file
string str = strStream.str();//str holds the content of the file
vector<string> result = explode(str,',');
for (size_t i = 0; i < result.size(); i++) {
buf[i] = result[i].c_str(); // Here is Error
cout << "\"" << result[i] << "\"" << endl;
}
system("pause");
这是我从文件中取数据:(0x68,0x32,0x01,0x7B,0x01,0x1F,0x00,0x00,0x00,0x02,0x00, 0x00,0x00,0x00)
这个'byte'类型是如何定义的?它不是标准C++的一部分... – 2014-10-16 13:36:53
您正试图在字节数组中存储指向字符串的指针。 1个字节不能包含整个字符串。我不确定你想要做什么。 – 2014-10-16 13:38:54
'byte'不是C++ 11的标准类型。你的意思是[int8_t](http://en.cppreference.com/w/cpp/types/整数)? – 2014-10-16 13:41:07