Get bytes from a string in C++
This post will discuss how to get bytes from a string in C++.
1. Using std::transform
Since C++11, we can use std::byte to represent the actual byte data. We can use the std::transform function to transform a string into a vector of bytes:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#include <iostream> #include <string> #include <algorithm> #include <cstddef> std::vector<std::byte> getBytes(std::string const &s) { std::vector<std::byte> bytes; bytes.reserve(std::size(s)); std::transform(std::begin(s), std::end(s), std::back_inserter(bytes), [](char const &c){ return std::byte(c); }); return bytes; } int main() { std::string s = "Hello, World"; std::vector<std::byte> bytes = getBytes(s); for (auto byte: bytes) { std::cout << std::to_integer<int>(byte) << ' '; } return 0; } |
Output:
72 101 108 108 111 44 32 87 111 114 108 100
We can even write custom logic for transforming a string to a vector of bytes using a range-based for loop. Here’s what the code would look like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <string> #include <algorithm> #include <cstddef> std::vector<std::byte> getBytes(std::string const &str) { std::vector<std::byte> bytes; for (char const &c: str) { bytes.push_back(std::byte(c)); } return bytes; } int main() { std::string str = "Hello, World"; std::vector<std::byte> bytes = getBytes(str); for (auto byte: bytes) { std::cout << std::to_integer<int>(byte) << ' '; } return 0; } |
Output:
72 101 108 108 111 44 32 87 111 114 108 100
2. Using c_str() function
A simple solution to get bytes from a string is using the c_str() function that returns read-only const char*.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <string> int main() { std::string str = "Hello, World"; char const *c = str.c_str(); std::cout << c << std::endl; // Hello, World return 0; } |
To get non-const memory having the write access, we can pass the const char* returned by c_str() to the strcpy() function and get a pointer to the char array. To get a char array instead, we can copy the data to bytes using the std::copy standard copy algorithm:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> #include <string> #include <algorithm> int main() { std::string str = "Hello, World"; unsigned char bytes[str.length()]; std::copy(str.begin(), str.end(), bytes); std::cout << bytes << std::endl; // Hello, World return 0; } |
To convert a string into a vector of bytes, we can use the range constructor of vector:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <string> #include <vector> int main() { std::string str = "Hello, World"; std::vector<char> bytes(str.begin(), str.end()); bytes.push_back('\0'); char *c = &bytes[0]; std::cout << c << std::endl; // Hello, World return 0; } |
That’s all about getting bytes from a string in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)