毫无疑问还有其他方法可以做到这一点,但我可能会选择最简单的:
std::vector<unsigned char> integers; // Has your list of bytes
integers.push_back(0x02);
integers.push_back(0xFF);
integers.push_back(0x00);
integers.push_back(0x10);
integers.push_back(0x01);
std::string str; // Will have your resulting string
for(unsigned int i=0; i < integers.size(); i++)
for(int j=0; j<8; j++)
str += ((integers[i]<<j) & 0x80 ? "1" : "0");
std::cout << str << "\n";
size_t begin = str.find("1");
if(begin > 0) str.erase(0,begin);
std::cout << str << "\n";
Run Code Online (Sandbox Code Playgroud)
在你提到你正在使用长整型或其他什么之前我写了这篇文章,但这实际上并没有改变太多。掩码和 j 循环变量需要更改,但除此之外,上面的方法应该可以工作。