Reputation: 69
I'm making a program that converts bytes from a string into a binary system and then store them into a vector. I need to use bitset to convert them. My question is: how can i store the results in a vector b? I thought about saving them one number by one number, but how the loop would look like?
string key = "codekeys";
char text;
vector<int> k;
vector<int> b;
void f() {
for(char& text : key) {
k.push_back(text);
}
cout << "k size: " << k.size() << endl;
for(int i=0; i<k.size(); i++) {
cout << k[i] << " in binary " << bitset<8> (k[i]) << endl;
}
}
Upvotes: 1
Views: 47
Reputation: 60440
If you make b a vector of bitsets then you can store them easily.
string key = "codekeys";
char text;
vector<int> k;
vector<bitset<8>> b;
void f() {
for(char& text : key) {
k.push_back(text);
b.push_back(bitset<8>(text)); // convert to bitset and store in b
}
cout << "k size: " << k.size() << endl;
for(int i=0; i<k.size(); i++) {
cout << k[i] << " in binary " << b[i] << endl; // print b
}
}
Upvotes: 1