I have trouble converting const char to byte. I'm reading file using ifstream and it gives me content as string then I'm converting string to const char using c_str(); and then trying insert it to byte array for packet sending purpose. I'm new to c++ and can't understand how I must convert char to byte and need your help guys. here is my piece of code please give me some advice
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");
This is data which i take from file : (0x68,0x32,0x01,0x7B,0x01,0x1F,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00 )
i did it by myself, now i will explain solution. so i wanted String(0x68,0x32,0x03,0x22 etc..) variable split per "," and then convert it into hex value after all input it into byte array as 16 bit hex values.
char buf[42]; // Define Packet
const char* fname = path.c_str(); // File Location
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,','); // Explode Per comma
for (size_t i = 0; i < result.size(); i++) { // loop for every exploded value
unsigned int x;
std::stringstream ss;
ss << std::hex << result[i]; // Convert String Into Integer value
ss >> x;
buf[i] = x;
printf(&buf[i],"%04x",x); //Convert integer value back to 16 bit hex value and store into array
}
system("pause");
thanks all for replay.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments