int i;
for( i = 0 ; i < str.length() ; i++ )
ans += ascii_to_bin(str[i]) + " ";
cout<<ans<<endl<<endl;
string ascii_to_bin(char ch)
{
// get the ascii value of ch
int ascii = (int)ch;
string bin = "";
// loop untill ascii becomes 0
while(ascii > 0)
{
if(ascii % 2 == 0)
bin = "0" + bin;
else
bin = "1" + bin;
ascii /= 2;
}
if(bin.length() < 8)
{
int i;
for( i = 0 ; i < 8 - bin.length() ; i++ )
bin = "0" + bin;
}
return bin;
}
Hi all I am currently doing an assignment and reading my friends code. it is to decrypt
Enter the string to encrypt: 011010100110010101101110
output = jen