This repository has been archived on 2025-03-26. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
mqttd/src/libmqttd/utilities/bytes.hpp
2024-04-21 17:02:18 -03:00

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_