The article here discusses various ways to convert an input string to a vector of bytes in C++.
Table Of Contents
Method 1: Using a For Loop
This program creates a string called str and a vector called bytes. It then iterates over the characters in the string and converts them to unsigned char (the size of an unsigned char is 1 byte) values using the static_cast() function. Finally, it prints out the integer value(ASCII value) of the vector of bytes.
Algorithm
- Include the necessary headers:
#include <iostream>
,#include <vector>
, and#include <string>
. These headers provide the necessary functions and classes for input/output, vectors, and strings, respectively. - Create a string object and a vector object.
- Iterate over the characters in the string and convert them to unsigned char values using the static_cast() function. This function allows you to convert a value from one type to another, provided that the conversion is safe and well-defined. In this case, we are converting the char values in the string to unsigned char values in the vector.
- You can print out the vector of bytes to verify that the conversion was successful.
Program
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string str = "Hello, world!"; vector<unsigned char> bytes; // convert the string to a vector of bytes for (char c : str) { bytes.push_back(static_cast<unsigned char>(c)); } // print the integer or ASCII value of vector of bytes for (unsigned char b : bytes) { cout << (int)b << " "; } cout << endl; return 0; }
Output
72 101 108 108 111 44 32 119 111 114 108 100 33
Time Complexity – O(N)
Frequently Asked:
- Convert a string to a vector of chars in C++
- Remove certain characters from a String in C++
- Convert a String to a Vector of Bytes in C++
Here, N is the length of the string. Since we iterate through each character of the string in the for loop and put each character (here unsigned char) in the vector.
Method 2: Using std::vector Constructor
std::vector has a constructor just for this purpose. The uint_8 is the same as a byte.
Algorithm
- Include the necessary headers:
#include <iostream>
,#include <vector>
, and#include <string>
. These headers provide the necessary functions and classes for input/output, vectors, and strings, respectively. - Create a string object and a vector object with uint_8 as the constructor.
- Provide an iterator to the start and end of the string as arguments to the vector.
- You can print out the vector of bytes to verify that the conversion was successful.
Program
#include <iostream> #include <vector> #include <string> using namespace std; int main() { //declare a string string str = "Hello, world!"; //use vector constructor to break the string into single bytes vector<uint8_t> bytes(str.begin(), str.end()); //print ASCII value of each byte for(int i=0; i<bytes.size(); i++) { cout<<(int)bytes[i]<<" "; } cout<<endl; return 0; }
Output
72 101 108 108 111 44 32 119 111 114 108 100 33
Time Complexity – O(N)
Here, N is the length of the string.
Since the iterator travels from the beginning of the string to the end separating each byte which in this case is each character of the string.
Method 3: Using std::transform method
The std::transform() function is a useful utility in C++ that allows you to apply a function to a range of elements and store the result in another range. It is a part of the algorithm header file. It can be used to convert a string to a vector of bytes as follows:
Algorithm
- Include the necessary headers:
#include <iostream>
,#include <vector>
,#include <string>
, and#include <algorithm>
. These headers provide the necessary functions and classes for input/output, vectors, strings, and algorithms, respectively. - Create a string object and a vector object.
- Use the std::transform function to apply a conversion function to each character in the string, and store the result in the vector. The std::back_inserter function() is used to insert the elements into the vector.
- You can print out the vector of bytes to verify that the conversion was successful.
Program
#include <iostream> #include <vector> #include <algorithm> //for std::transform using namespace std; int main() { string str = "Hello, world!"; vector<unsigned char> bytes; // transform characters in string to vector of bytes transform( begin(str), end(str), back_inserter(bytes), [](char c){ return (unsigned char)(c); }); for (unsigned char b: bytes) { std::cout << (int)(b) << ' '; } return 0; }
Output
72 101 108 108 111 44 32 119 111 114 108 100 33
Time Complexity – O(N)
N is the length of the string.
Insert all characters inside the vector from the beginning of the string to the end.
Method 4: Using std::copy() method
This program first creates a string called str and a vector of bytes called bytes. It then resizes the vector to the size of the string. Next, it uses the std::copy() algorithm to copy the data from the string into the vector. Finally, it iterates over the vector and prints each byte.
Program
#include <iostream> #include <vector> #include <string> using namespace std; int main() { string str = "Hello, World!"; vector<unsigned char> bytes; // Resize the vector to the size of the string bytes.resize(str.size()); // Copy the string data into the vector copy(str.begin(), str.end(), bytes.begin()); for (unsigned char b: bytes) { cout << (int)(b) << ' '; } return 0; }
Output
72 101 108 108 111 44 32 119 111 114 108 100 33
Time Complexity – O(N)
Summary
In this article, we learned how to convert an input string to a vector of bytes using a for loop and the vector::push_back()
function, the inbuilt constructor uint_8
in the vector class, the std::tranform() function and the std::copy() functions. We verified by printing the ASCII value of each byte. Thanks.