首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

从std :: vector <bool>获取字节

std::vector<bool>获取字节是指将std::vector<bool>中的元素转换为字节(unsigned char)。这里是一个简单的方法,将std::vector<bool>转换为std::vector<unsigned char>

代码语言:cpp
复制
#include<iostream>
#include<vector>

std::vector<unsigned char> convertVector(const std::vector<bool>& input) {
    std::vector<unsigned char> output;
    int byte = 0;
    int bit_index = 0;

    for (const bool bit : input) {
        if (bit_index == 8) {
            output.push_back(byte);
            byte = 0;
            bit_index = 0;
        }

        byte |= static_cast<unsigned char>(bit) << (7 - bit_index);
        bit_index++;
    }

    if (bit_index > 0) {
        output.push_back(byte);
    }

    return output;
}

int main() {
    std::vector<bool> input = {1, 0, 1, 1, 0, 1, 0, 1};
    std::vector<unsigned char> output = convertVector(input);

    for (const unsigned char byte : output) {
        std::cout<< static_cast<int>(byte) << " ";
    }

    return 0;
}

这个代码示例将std::vector<bool>中的元素转换为字节,并将结果存储在std::vector<unsigned char>中。在这个例子中,输入向量{1, 0, 1, 1, 0, 1, 0, 1}将被转换为字节向量{192}

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

【计算机网络】序列化与反序列化

位置开始寻找分隔符sep,找到分割符sep后,将区间内的子串插入vector数组中 当sep为空格时,只占用一个位置,pos处于空格位置 ,只需加1即可跳出空格 故start的位置 只需 pos 位置...的实现 输入 man recv 第一个参数为 套接字 第二个参数为缓冲区 第三个参数 为缓冲区长度 第四个参数为 读取方式 ,一般默认为0 返回值为读取到的字节数,若字节数小于0,则表示读取出错...后面先减去一个分隔符,再减去有效载荷的长度 有效载荷位置开始 取 有效载荷的长度个字符 即 取到有效载荷 3....则为true 分割失败,则为false static bool StringSplit(const string &str,const string &sep,std::vector<string...Deserialize(const std::string &instr) { #ifdef MYSELF std::vector result

21110

BoostAsyncSocket 异步反弹通信案例

Boost 利用ASIO框架实现一个跨平台的反向远控程序,该远控支持保存套接字,当有套接字连入时,自动存储到map容器,当客户下线时自动map容器中移除,当我们需要与特定客户端通信时,只需要指定客户端...,发送数据则是通过同步的方式进行,当我们需要发送数据时,只需要将数据字符串放入到一个BYTE*字节数组中,并在调用tcpServer.Send时将所需参数,套接字ID,缓冲区Buf数据,以及长度传递即可实现将数据发送给指定的客户端...> #include #include using namespace std; // 存储当前客户端的ID号 std::vector...链表 std::vector vecSegTag; for (CustonTokenizer::iterator beg = tok.begin(); beg !...::cout << "已连接到服务端." << std::endl; // 循环接收命令 while (1) { // 验证地址端口是否开放,默认等待5秒 bool

90420

C++入门到精通——string类

注意,这个类独立于所使用的编码来处理字节:如果用来处理多字节或变长字符(如UTF-8)的序列,这个类的所有成员(如长度或大小)以及它的迭代器,将仍然按照字节(而不是实际编码的字符)来操作。...= myString.end(); ++it) { char ch = *it; // 获取迭代器指向的字符 std::cout << ch << std::endl;...因此,typeid(std::string::iterator).name() 的作用就是获取 std::string::iterator 的类型名,并输出到标准输出流中。...其次:还有一个size_t字段保存字符串长度,一个size_t字段保存堆上开辟空间总的容量 最后:还有一个指针做一些其他事情。 故总共占16+4+4+4=28个字节。...at()函数可以用于访问std::vectorstd::array、std::deque以及std::string等容器。

16510

BoostAsyncSocket 异步反弹通信案例

Boost 利用ASIO框架实现一个跨平台的反向远控程序,该远控支持保存套接字,当有套接字连入时,自动存储到map容器,当客户下线时自动map容器中移除,当我们需要与特定客户端通信时,只需要指定客户端...,当我们需要发送数据时,只需要将数据字符串放入到一个BYTE*字节数组中,并在调用tcpServer.Send时将所需参数,套接字ID,缓冲区Buf数据,以及长度传递即可实现将数据发送给指定的客户端;/...>#include #include using namespace std;// 存储当前客户端的ID号std::vector...链表std::vector vecSegTag;for (CustonTokenizer::iterator beg = tok.begin(); beg !...connect_success);timer_.cancel();return connect_success;}// 验证服务器端口是否开放bool port_is_open(std::string

81950
领券