C strings size and array -
i facing problem . have following sample
std::string key = "30 14 06 03 55 04 03 14 0d 2a"; when found size of key string
size_t sizee = key.size(); it comes out 29 that's fine.
but want output
char data[10]; data[0] = 0x30; data[1] = 0x14; data[2] = 0x06; data[3] = 0x03; data[4] = 0x55; data[5] = 0x04; data[6] = 0x03; data[7] = 0x14; data[8] = 0x0d; data[9] = 0x2a; the size should come 10 considering 30 1 14 two. size should size of array if string become 00 01 array size should two.
#include <iostream> #include <sstream> #include <vector> #include <iomanip> using namespace std; int main(){ string key = "30 14 06 03 55 04 03 14 0d 2a"; istringstream iss(key); vector<char> data; unsigned x; iss >> hex; while(iss >> x){ data.push_back(x); } size_t size = data.size(); cout << "char data[" << size << "];" << endl; for(int i=0;i < size ; ++i){ cout << "data[" << << "] = 0x" << hex << uppercase << setw(2) << setfill('0') << (unsigned)data[i] << ';' << endl; } } #include <iostream> #include <sstream> #include <vector> #include <iomanip> #include <cstdlib> using namespace std; int main(){ string key = "30 14 06 03 55 04 03 14 0d 2a"; istringstream iss(key); vector<char> data; unsigned x; iss >> hex; while(iss >> x){ data.push_back(x); } size_t size = data.size(); cout << "char content[" << size << "];" << endl; unsigned *content; content = (unsigned*)malloc(size*sizeof(unsigned)); for(int i=0;i < size ; ++i){ //cout << "data[" << << "] = 0x" << hex << uppercase << setw(2) << setfill('0') << (unsigned)data[i] << endl; content[i]=data[i]; cout << "content[" << << "] = 0x" << hex << uppercase << setw(2) << setfill('0') << content[i] << endl; } if(content[0] == 0x30) cout << "i it." << endl; free(content); }
Comments
Post a Comment