In C#, we have following functions to convert a string to a UTF-8 encoded sequence of bytes and vice-versa:
Encoding.UTF8.GetString(Byte[])
Encoding.UTF8.GetBytes(Char[])
/Encoding.UTF8.GetBytes(String)
I am trying to achieve the same thing in C++, as follows:
std::string GetStringFromBytes(std::vector<uint8_t> bytes){
std::string str(bytes.begin(), bytes.end());
return str;
}
std::vector<uint8_t> GetBytesFromString(const std::string& str){
std::vector<uint8_t> bytes(str.begin(), str.end());
return bytes;
}
Is this approach correct? I'm assuming that the string that I'm converting is already in UTF-8 format.