user1398405
user1398405

Reputation: 63

Converting a decimal array into a 2D 16bit character array in C++

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!

Upvotes: 0

Views: 619

Answers (1)

David
David

Reputation: 1419

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

                }

Upvotes: 1

Related Questions