python text = text.encode(“utf-8”) C++实现
std::string char_to_hex(unsigned char ch) {
std::stringstream ss;
ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(ch);
return ss.str();
}
// 将 std::string 对象转换为 UTF-8 编码的十六进制字符串表示形式
std::string string_to_utf8_hex(const std::string& text) {
std::string utf8Hex;
for (char ch : text) {
utf8Hex += char_to_hex(static_cast<unsigned char>(ch));
}
return utf8Hex;
}
int main() {
std::string text = "10500";
std::string utf8Hex = string_to_utf8_hex(text);
std::cout << "UTF-8 encoded text: " << utf8Hex << std::endl;
return 0;
}