how can replace and add to array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
 
// function to convert decimal to binary
void decToBinary(int n)
{
    // array to store binary number
    int binaryNum[32];
 
    // counter for binary array
    int i = 0;
    while (n > 0) {
 
        // storing remainder in binary array
        binaryNum[i] = n % 2;
        n = n / 2;
        i++;
    }
 
    // printing binary array in reverse order
    for (int j = i - 1; j >= 0; j--)
        cout << binaryNum[j];
}
 
// Driver program to test above function
int main()
{
    int n = 10000;
    decToBinary(n);
    return 0;
}



Output:
10011100010000

I want change out put like this [ by code] :
110111000100010


Last edited on
Hawlong wrote:
Output:
10011100010000

I want change out put like this [ by code] :
110111000100010



Would you like to have another go at explaining what you want to do.
Last edited on
Well 10000 decimal is 10011100010000 as binary - so what's the issue?

Do you mean you want the output left filled with 0's like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

// function to convert decimal to binary
void decToBinary(int n) {
	// array to store binary number
	int binaryNum[32] {};

	// counter for binary array
	for (size_t i {}; n > 0; ++i, n /= 2)
		// storing remainder in binary array
		binaryNum[i] = n % 2;

	// printing binary array in reverse order
	for (size_t j = 32; j > 0; --j)
		std::cout << binaryNum[j - 1];
}

// Driver program to test above function
int main() {
	decToBinary(10000);
}



00000000000000000010011100010000


This can also be done simply using std::bitset:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <bitset>

// function to convert decimal to binary
void decToBinary(unsigned n) {
	std::cout << std::bitset<32> {n} << '\n';
}

// Driver program to test above function
int main() {
	decToBinary(10000);
}


See:
https://cplusplus.com/reference/bitset/bitset/
Last edited on
Thanks all for replay ,


I have a packet that comes encrypted from the server
to decode this packet , I convert it to binary and i change some bits ..

Example:
i have this bits : 10011100010000
i want add (1)bit 1001110(1)0010000 and change second bit to (1)
1(1)0111010010000

Thanks ..


Either use a std::string or a vector<bool>. Then you can corrupt your messages to your heart's content.
Well changing 100111 to 110111 is easy. Just change the appropriate element in the array. Adding an extra bit is best done with using a std::vector. Or use the std::bitset:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <bitset>

// function to convert decimal to binary
void decToBinary(unsigned n) {
	constexpr size_t noBits { 16 };
	constexpr size_t inspos { noBits - 8 };

	static_assert (noBits >= 16 && noBits % 8 == 0);

	const auto s { std::bitset<noBits> { n }.to_string() };

	std::bitset<noBits> bar { s.substr(1, inspos) + "1" + s.substr(inspos + 1) };
	bar.set(13, 1);

	std::cout << bar << '\n';
}

// Driver program to test above function
int main() {
	decToBinary(10000);
}


which displays:


0110111010010000


as required.
Last edited on
Thanks so much seeplus , solved
Topic archived. No new replies allowed.