简体   繁体   English

如何将一个位集分配给一个无符号的字符向量?

[英]How to Assign a bitset to an unsigned char vector?

I am using an unsigned char vector which has some hex values. 我正在使用具有一些十六进制值的无符号char向量。

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

Then, using a bitset I "morph" the sync[3] to an 8 bit representation.This is because I need to corrupt/toggle any random bit in it. 然后,使用位集将“ sync [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);

Since, I am passing my vector by reference, I wanted to make changes to it. 由于我通过参考传递了矢量,因此我想对其进行更改。 ie whatever changes I have made to my bitset, I wanted to commit it to the vector too. 即,无论我对位集进行了什么更改,我也都希望将其提交给向量。 However, I am not sure how to convert a bitset to an unsigned char and how to assign it to my vector, in order to update my vector sync . 但是,我不确定如何将位集转换为未签名的char以及如何将其分配给我的向量,以便更新我的向量sync

Call manipulator.to_ulong() . 调用manipulator.to_ulong() (The narrowing conversion to your unsigned char will be harmless.) (缩小到unsigned char转换将是无害的。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM