35 lines
857 B
C++
35 lines
857 B
C++
#ifndef INCLUDE_UTILITIES_BYTES_HPP_
|
|
#define INCLUDE_UTILITIES_BYTES_HPP_
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace utilities {
|
|
namespace bytes {
|
|
|
|
/**
|
|
* @brief Convert a string to a vector of bytes.
|
|
*
|
|
* @param value The string to convert.
|
|
* @return A vector containing the bytes of the string.
|
|
*/
|
|
std::vector<std::byte> to_bytes(const std::string &value);
|
|
|
|
/**
|
|
* @brief Convert a value to a vector of bytes.
|
|
*
|
|
* @tparam T The type of the value.
|
|
* @param value The value to convert.
|
|
* @return A vector containing the bytes of the value.
|
|
*/
|
|
template <typename T>
|
|
std::vector<std::byte> to_bytes(const T &value) {
|
|
const std::byte *byte_ptr = reinterpret_cast<const std::byte *>(&value);
|
|
return std::vector<std::byte>(byte_ptr, byte_ptr + sizeof(T));
|
|
}
|
|
|
|
} // namespace bytes
|
|
} // namespace utilities
|
|
|
|
#endif // INCLUDE_UTILITIES_BYTES_HPP_
|