1 c++
我有一个字符串值:
std::string bl="0x"+"a0";//其中 a0 是一个 heva 数字。我添加了 0x 因为我想要我的向量
std::vector<unsigned char>vect;
Run Code Online (Sandbox Code Playgroud)
vect.push_back(bl.begin(), bl.end());//错误不起作用。
需要帮忙。该怎么办?我正在使用 ubuntu c++ 代码。
由于你的 string 的内容,我不能 100% 确定你想要什么bl。
从字面上理解:
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
Run Code Online (Sandbox Code Playgroud)
或者您可以使用构造函数:
std::string bl = "0xA0";
std::vector<unsigned char> vect(bl.begin(), bl.end());
// ^ you use ranges with the constructor too
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,向量都包含字符“0”、“x”、“A”和“0”。
或者,您可能希望字符串包含 ASCII 值为 (in hex) 的单个字符0xA0。如果这样的话,"0x"+"a0"那就大错特错了。
std::string bl = "\xA0";
std::vector<unsigned char> vect(bl.begin(), bl.end());
Run Code Online (Sandbox Code Playgroud)
该向量包含一个字符,其 ASCII 值为0xA0。
我希望这有帮助。