Enter a string or a name, this program will convert the string into binary format. Let me explain how the code works, It will convert each character to its decimal representation and then convert the decimal representation to binary representation, the output '0' (in computers, each character needs 1 byte = 8 bits so,when you convert it to binary representation  you need to complete the missing bits).

Program:


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    cin>>a;
    for(int i=0;i<int(a.size());i++)
    {

        //convert each character to its decimal representation
        int b=int(a[i]);
        vector<int> c;

        //convert from dec to bin
        while(b!=0)
        {
            c.push_back(b%2);
            b/=2;
        }

        //your name characters
        cout<<a[i]<<" = ";

        //completing the missing bits
        for(int j=0;j<int(8-c.size());j++)
            cout<<0;

        //the output
        for(int j=c.size()-1;j>=0;j--)
            cout<<c[j];
        cout<<"\n";
    }
    cout<<"\nif you like it!!!please share our post.";
    return 0;
}


OUTPUT:



Post a Comment

أحدث أقدم