c++ - How to convert an array of decimals to an array of binary numbers? -


*array have is

int array{9} = { 2 ,2, 10 , 3 , 1, 15, 12, 6 ,1}; 

i trying covert array of binary, should

int array2[35]= {0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,0, 0, 1, 1, 0, 0, 0, 1}; 

code using is:

double decimal2binary(long m) {     int remainder;     long binary = 0, = 1;     while(m != 0)     {         remainder = m%2;         m = m/2;         binary= binary + (remainder*i);         = i*10;     }     return binary; } 

the problem , when converting 2 binary ,the output 10. want 0010 , 7 giving 111 in place of 0111. else need do. please help*

you want exact 4 binary digit?

double decimal2binary(long m) { int remainder; long binary = 0, = 1; while(m != 0) {     remainder = m%2;     m = m/2;     binary= binary + (remainder*i);     = i*10; } return binary; } 

this function great, know used long , while function. how can exact 4 digit binary? use string

warning, converting function 4 digit string result in function max procesing 1111 -> 15

string decimal2binary(long m) {     int remainder;     string binary = "";     while(m != 0)     {         remainder = m%2;         m = m/2;         if(remainder == 0){             binary = '0' + binary;         }         else{             binary = '1' + binary;         }     }     while(binary.size() != 4){         binary = '0' + binary;     }     return binary; } 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -