如何在数组中存储二进制数?



我一直在研究这个它是加密软件的一部分类似于2fa

#include <iostream>
#include <cstdio>     
#include <cstdlib>   
#include <ctime>    
using namespace std;
int main()
{

int RGX;
int box[32];
srand (time(NULL));

RGX = rand() % 100000000 + 9999999;
cout << "Random Generated One Time HEX #:" << endl;
cout << std::hex << RGX << endl;

while(RGX!=1 || 0)
{
int m = RGX % 2;
cout << " " << m << " ";

RGX = RGX / 2;

cout << RGX << endl;

} 
return 0;
}

下面是它输出的示例:

Random Generated One Time HEX #:
3ff3c70
0 1ff9e38
0 ffcf1c
0 7fe78e
0 3ff3c7
1 1ff9e3
1 ffcf1
1 7fe78
0 3ff3c
0 1ff9e
0 ffcf
1 7fe7
1 3ff3
1 1ff9
1 ffc
0 7fe
0 3ff
1 1ff
1 ff
1 7f
1 3f
1 1f
1 f
1 7
1 3
1 1

** Process exited - Return Code: 0 **

结果每次都不同,因为它是随机的,我仍然没有完成。但是我需要知道的是如何将二进制值存储在数组中,二进制值是左边的数字。

您可以使用std::bitset而不是手动提取位和数组:

#include <iostream>
#include <ctime> 
#include <cstdlib>   
#include <bitset>
int main() {
srand (time(NULL));
int RGX = rand() % 100000000 + 9999999;
std::cout << "Random Generated One Time HEX #: n";
std::cout << std::hex << RGX << "n";
std::bitset<32> box(RGX);
for (int i=0;i<32;++i){
std::cout << box[i];
}

}

可能的输出:

Random Generated One Time HEX #: 
478ada7
11100101101101010001111000100000

&quot后括号内无而(RGX !=1 || 0) "它使用%并除以2,直到得到1或0。

。那个条件不是这么说的。条件为"loop while (RGX不等于1)或0"由于0在转换为bool时总是false,因此您的条件相当于while(RGX != 1)

您可以使用(不知道为什么要这样做)std::bitset来存储未打包的比特集合。在RNG设施中最好使用<random>

#include <iostream>
#include <cstdlib>
#include <bitset>
#include <random>
using std::cout;
int main()
{
std::random_device rd;
std::uniform_int_distribution<int> dist(0, 9999999);
unsigned RGX = dist(rd);
cout << "Random Generated One Time HEX #:" << std::endl;
std::bitset<32> bits {RGX}; // E.g. bits[5] would give you 5th bit

cout << std::hex << RGX << " contains "<< bits << std::endl;  
return 0;
}

最新更新