[英]String vector to uint8_t C++
我有一个向量, 其中的字符串表示位,如下所示:
string str1[] = { "0b01100101", "0b01100101", "0b01011101", "0b11001111"}
我需要将精确值添加到uint8_t位向量中:
uint8_t str2[] = { 0b01100101, 0b01100101, 0b01011101, 0b11001111}
最终结果应与上面完全一样。 如果有人知道我该怎么做,我将不胜感激。
不幸的是,没有标准函数来解析带有“ 0b”前缀的二进制字符串。
您可以使用旧的std::strtoul
(调用std::strtoul
1行和5条错误检查的行):
#include <algorithm>
#include <stdexcept>
#include <cstdlib>
#include <string>
uint8_t binary_string_to_uint8(std::string const& s) {
if(s.size() != 10 || '0' != s[0] || 'b' != s[1])
throw std::runtime_error("Invalid bit string format: " + s);
char* end = 0;
auto n = std::strtoul(s.c_str() + 2, &end, 2);
if(end != s.c_str() + s.size())
throw std::runtime_error("Invalid bit string format: " + s);
return n;
}
int main() {
std::string str1[] = { "0b01100001", "0b01100101", "0b01011101", "0b11001111"};
uint8_t str2[sizeof str1 / sizeof *str1];
std::transform(std::begin(str1), std::end(str1), std::begin(str2), binary_string_to_uint8);
}
请注意,它们可能会输出与算法几乎相同或相同的程序集,因此几乎总是首选像其他答案中那样的算法。 尽管如此,这里还有一些使用循环的选项:
std::stoul
至少需要C ++ 11。 我们在这里也没有边界检查,我们假设所有字符串的大小均>=
2。
std::string str1[] = {"0b01100101", "0b01100101", "0b01011101", "0b11001111"};
const size_t sz = sizeof str1 / sizeof *str1;
uint8_t str2[sz];
for (size_t i = 0; i < sz; ++i)
str2[i] = static_cast<uint8_t>(std::stoul(&str1[i][2], nullptr, 2));
由于实际上它们很可能是可变大小的数组,因此在这里使用实际的vector
类型可能会更好。
std::vector<std::string> vs;
// add a bunch of stuff to vs
...
std::vector<uint8_t> vi;
vi.reserve(vs.size());
for (const auto &s : vs)
vi.push_back(static_cast<uint8_t>(std::stoul(&s[2], nullptr, 2)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.