0

i want to convert decimal values to 16 bit binary values. i used this code in another one.

      #include <iostream>
      #include <bitset>

      int main() { 
      int x = 5;

      std::bitset<8> bin_x(x);
      std::cout << bin_x;

      return 0;
      }

this is a code posted by a member. i want to use it in a loop and store the value of bin_x in a 16 two dimensional character array. how can it be done? here is what iam doing

    #include<iostream>
    using namespace std;
    #include <bitset>

     int main(){

int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal   numbers.

const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
 //strcpy(BinaryArray[i], "0000000000000000");
char BinaryArray[ArrayLen][16]; //Create an array of the same length for binary nos.


for(int i = 0; i<ArrayLen; i++)
{

    int CurrentDec = DecimalArray[i]; //Store current Decimal number in  CurrentDec variable

    strcpy(BinaryArray[i], "0000000000000000");

std::bitset<16> bin_x(CurrentDec);
cout<< "bin"<<bin_x<< endl;
for (int j = 0; j<15; j++)
         {
 bin_x=BinaryArray[i][j];


    cout<< "b1"<< BinaryArray[i] << endl;
     }


cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
cout<<"Decimal  Binary \n\n";
}

//Output both arrays
for( i = 0; i<ArrayLen; i++){
    cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
}

cin.get();
return 0;

      }
       but i do not get the value in BinaryArray. kindly help me with it, its very urgent. Thanks!

1 Answer 1

1
           #include<iostream>

           using namespace std;

           #include <bitset>

           int main(){

           int DecimalArray[] = {1,2,3,4,5,22,555,85,18,741}; //Create an array of decimal numbers.

            const int ArrayLen = sizeof(DecimalArray)/sizeof(int); //Store the size of the Decimal Array in a constant
           //strcpy(BinaryArray[i], "0000000000000000");
           char BinaryArray[ArrayLen][17]; //Create an array of the same length for binary nos.

           int i;

           for(i = 0; i<ArrayLen; i++)
            {

           int CurrentDec = DecimalArray[i]; //Store current Decimal number in       CurrentDec variable
           int index = 1, CurrentBin = 0;
           strcpy(BinaryArray[i], "0000000000000000");

           std::bitset<16> bin_x(CurrentDec);
           cout<< "bin"<<bin_x<< endl;
           for (int j = 0; j<16; j++)
           {
            if (bin_x[15-j])
               {

                 BinaryArray[i][j] = '1';
                  }



                  cout<< "b1"<< BinaryArray[i][j]<<endl ;

                    }

                     }


               cout<<"The Decimal numbers and their Binary Equivalents are:\n\n";
               cout<<"Decimal Binary \n\n";


               //Output both arrays
               for( i = 0; i<ArrayLen; i++){
               cout<<DecimalArray[i]<<"\t "<<BinaryArray[i]<<endl;
                }



               cin.get();
               return 0;

                }
Sign up to request clarification or add additional context in comments.

8 Comments

no, its not working. i want a 16 bit binary for the decimal values. for exmaple for 1 i need 0000000000000001 in binary array
What happens when you replace your line "bin_x = BinaryArray[i][j]" with my suggested "BinaryArray[i][j] = bin_x[j]"?
i only get '0' in BinaryArray for every iteration
OK, I updated my answer. This should work better for you. (Note that you may also want to replace bin_x[j] with bin_x[15-j] so that you don't get the binary representation written in reverse.)
How am i suppose to check that iam getting correct values in binaryarray. i think it gives the bit on the ith and jth position, it doesnot give the whole 16 bit character
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.