PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 8, 2022

[FIXED] How do i store a binary number in an array?

 August 08, 2022     arrays, binary, c++, decimal, random     No comments   

Issue

Ok, so i been working on this right here that is intended to be part of a encryption software that works synonymously like 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;
}

Here is a sample of what it outputs:

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 **

The result is different each time since it's randomized, i'm still not finished. But what i need to know is how do i store the binary value in an array, the binary value are the numbers on the left.


Solution

You can use a std::bitset instead of manually extracting bits and the array:

#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];
    }
 
}

Possible output:

Random Generated One Time HEX #: 
478ada7
11100101101101010001111000100000

No inside the brackets after " while(RGX!=1 || 0) " it uses % and divides by 2 up until it gets to 1 or 0.

No. Thats not what that condition says. The condition says "loop while (RGX is not equal to 1) or 0". As 0 is always false when converted to bool, your condition is equivalent to while(RGX != 1).



Answered By - 463035818_is_not_a_number
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing