如何将bitset分配给未签名的char矢量

How to Assign a bitset to an unsigned char vector?

本文关键字:char 矢量 bitset 分配      更新时间:2023-10-16

我正在使用具有一些十六进制值的无符号char矢量。

   std::vector<unsigned char> sync;
   sync.push_back(0x50);
   sync.push_back(0x51);
   sync.push_back(0x52);
   sync.push_back(0x53);

然后,使用bitset i"变形"同步[3]到8位表示。这是因为我需要在其中损坏/切换任何随机位。

    srand(time(NULL));
    int bitsetIndex= random()%8; 
    std::bitset<8> manipulator(v[3]); //v is the vector argument which takes "sync" vector
                                      //by reference 
    manipulator.flip(bitsetIndex);

由于我是通过引用传递我的向量,所以我想对其进行更改。IE。无论我对BITSet做过什么更改,我也都想将其提交给向量。但是,我不确定如何将BITSet转换为未签名的字符以及如何将其分配给我的向量,以更新我的向量同步

调用manipulator.to_ulong()。(向您的unsigned char的狭窄转换将是无害的。)