gpt4 book ai didi

C++ 转换字符串- unsigned char

转载 作者:行者123 更新时间:2023-11-28 03:53:03 24 4
gpt4 key购买 nike

我有一个字符串值:std::string bl="0x"+"a0";//其中 a0 是一个 heva 数。我确实添加了 0x 因为我想要我的 vector

std::vector<unsigned char>vect;

vect.push_back(bl.begin(), bl.end());//错误不起作用。

需要帮助。该怎么办?我正在使用 ubuntu c++ 代码。

最佳答案

由于字符串 bl 的内容,我不能 100% 确定您想要什么。

从字面上看:

std::string bl = "0xA0";
// ^ this is what you meant to write ("0x"+"A0" is actually adding pointers)

std::vector<unsigned char> vect;
vect.insert(vect.begin(), bl.begin(), bl.end());
// ^ you use ranges with .insert not push_back

或者你可以使用构造函数:

std::string bl = "0xA0";
std::vector<unsigned char> vect(bl.begin(), bl.end());
// ^ you use ranges with the constructor too

在这两种情况下, vector 都包含字符“0”、“x”、“A”和“0”。

或者,您可能希望字符串包含单个字符,其 ASCII 值为(十六进制)0xA0。如果是这样,"0x"+"a0" 就大错特错了。

std::string bl = "\xA0";
std::vector<unsigned char> vect(bl.begin(), bl.end());

vector 包含一个字符,其ASCII值为0xA0

希望对您有所帮助。

关于C++ 转换字符串- unsigned char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4733928/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com