在C ++中将字符转换为二进制

时间:2015-09-12 12:39:49

标签: c++ loops binary chars

我写了这篇文章,当我发布它时,它给了我所有的可能性。我希望它只是给我一个与我输入的字符相对应的那个。你能告诉我这里有什么问题吗? 提前谢谢。

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
char letter='b';
while(letter!='end')
{
    cout<<"What is the letter you wanna convert?"<<endl;
    cin >> letter;
    if(letter='A') {cout<<"01000001"<<endl;}
    if(letter='B') {cout<<"01000010"<<endl;}
    if(letter='C') {cout<<"01000011"<<endl;}
    if(letter='D') {cout<<"01000100"<<endl;}
    if(letter='E') {cout<<"01000101"<<endl;}
    if(letter='F') {cout<<"01000110"<<endl;}
    if(letter='G') {cout<<"01000111"<<endl;}
    if(letter='H') {cout<<"01001000"<<endl;}
    if(letter='I') {cout<<"01001001"<<endl;}
    if(letter='J') {cout<<"01001010"<<endl;}
    if(letter='K') {cout<<"01001011"<<endl;}
    if(letter='L') {cout<<"01001100"<<endl;}
    if(letter='M') {cout<<"01001101"<<endl;}
    if(letter='N') {cout<<"01001110"<<endl;}
    if(letter='O') {cout<<"01001111"<<endl;}
    if(letter='P') {cout<<"01010000"<<endl;}
    if(letter='Q') {cout<<"01010001"<<endl;}
    if(letter='R') {cout<<"01010010"<<endl;}
    if(letter='S') {cout<<"01010011"<<endl;}
    if(letter='T') {cout<<"01010100"<<endl;}
    if(letter='U') {cout<<"01010101"<<endl;}
    if(letter='V') {cout<<"01010110"<<endl;}
    if(letter='W') {cout<<"01010111"<<endl;}
    if(letter='X') {cout<<"01011000"<<endl;}
    if(letter='Y') {cout<<"01011001"<<endl;}
    if(letter='Z') {cout<<"01011010"<<endl;}

    if(letter='a') {cout<<"01100001"<<endl;}
    if(letter='b') {cout<<"01100010"<<endl;}
    if(letter='c') {cout<<"01100011"<<endl;}
    if(letter='d') {cout<<"01100100"<<endl;}
    if(letter='e') {cout<<"01100101"<<endl;}
    if(letter='f') {cout<<"01100110"<<endl;}
    if(letter='g') {cout<<"01100111"<<endl;}
    if(letter='h') {cout<<"01101000"<<endl;}
    if(letter='i') {cout<<"01101001"<<endl;}
    if(letter='j') {cout<<"01101010"<<endl;}
    if(letter='k') {cout<<"01101011"<<endl;}
    if(letter='l') {cout<<"01101100"<<endl;}
    if(letter='n') {cout<<"01101110"<<endl;}
    if(letter='o') {cout<<"01101111"<<endl;}
    if(letter='p') {cout<<"01110000"<<endl;}
    if(letter='q') {cout<<"01110001"<<endl;}
    if(letter='r') {cout<<"01110010"<<endl;}
    if(letter='s') {cout<<"01110011"<<endl;}
    if(letter='t') {cout<<"01110100"<<endl;}
    if(letter='u') {cout<<"01110101"<<endl;}
    if(letter='v') {cout<<"01110110"<<endl;}
    if(letter='w') {cout<<"01110111"<<endl;}
    if(letter='x') {cout<<"01111000"<<endl;}
    if(letter='y') {cout<<"01111001"<<endl;}
    if(letter='z') {cout<<"01111010"<<endl;}
    getche();
}
return 666;

}

3 个答案:

答案 0 :(得分:3)

至于你的标题,忽略代码中的琐碎错误,最简单的解决方案是

cout<<"What is the letter you wanna convert?"<<endl;
cin >> letter;
cout << bitset<8>(letter).to_string() << endl;

无需进行硬编码转换。

另请注意,如果此问题是出于学术原因,并且您被要求开发使用通用算法生成字符串的解决方案,那么您的解决方案将获得 F

您可能希望在循环中正确使用℅2模运算(这不是您最近的演讲的一部分吗?)。

如果没有,我强烈建议采用std::bitset解决方案,而不是重新发明轮子。

答案 1 :(得分:2)

您需要if (letter == ...),而不是if (letter = ...)

&#34;你能告诉我这里有什么问题吗?&#34;

当你这样做时:

if (foo = bar)

...您正在影响barfoo的值。如果生成的bar值不为零,则条件为真。

欢迎编程。

答案 2 :(得分:0)

编码时-

if(a == anything){statements},它检查您的代码,如果是真的,它将执行您的语句

当您编码-

如果(a =任何东西){statements},那么它将值(任何东西)赋给变量(a)并执行您的语句

<pre><code>

#include <iostream>
#include <conio.h>
using namespace std;
int main()
{char letter='b';
cout<<"letter --> binary\nenter 0 to exit\n\n";
(letter!='0')
{
cout<<"\nWhat is the letter you wanna convert? - ";
cin >> letter;
if(letter=='A') {cout<<"01000001"<<endl;}
if(letter=='B') {cout<<"01000010"<<endl;}
if(letter=='C') {cout<<"01000011"<<endl;}
if(letter=='D') {cout<<"01000100"<<endl;}
if(letter=='E') {cout<<"01000101"<<endl;}
if(letter=='F') {cout<<"01000110"<<endl;}
if(letter=='G') {cout<<"01000111"<<endl;}
if(letter=='H') {cout<<"01001000"<<endl;}
if(letter=='I') {cout<<"01001001"<<endl;}
if(letter=='J') {cout<<"01001010"<<endl;}
if(letter=='K') {cout<<"01001011"<<endl;}
if(letter=='L') {cout<<"01001100"<<endl;}
if(letter=='M') {cout<<"01001101"<<endl;}
if(letter=='N') {cout<<"01001110"<<endl;}
if(letter=='O') {cout<<"01001111"<<endl;}
if(letter=='P') {cout<<"01010000"<<endl;}
if(letter=='Q') {cout<<"01010001"<<endl;}
if(letter=='R') {cout<<"01010010"<<endl;}
if(letter=='S') {cout<<"01010011"<<endl;}
if(letter=='T') {cout<<"01010100"<<endl;}
if(letter=='U') {cout<<"01010101"<<endl;}
if(letter=='V') {cout<<"01010110"<<endl;}
if(letter=='W') {cout<<"01010111"<<endl;}
if(letter=='X') {cout<<"01011000"<<endl;}
if(letter=='Y') {cout<<"01011001"<<endl;}
if(letter=='Z') {cout<<"01011010"<<endl;}

if(letter=='a') {cout<<"01100001"<<endl;}
if(letter=='b') {cout<<"01100010"<<endl;}
if(letter=='c') {cout<<"01100011"<<endl;}
if(letter=='d') {cout<<"01100100"<<endl;}
if(letter=='e') {cout<<"01100101"<<endl;}
if(letter=='f') {cout<<"01100110"<<endl;}
if(letter=='g') {cout<<"01100111"<<endl;}
if(letter=='h') {cout<<"01101000"<<endl;}
if(letter=='i') {cout<<"01101001"<<endl;}
if(letter=='j') {cout<<"01101010"<<endl;}
if(letter=='k') {cout<<"01101011"<<endl;}
if(letter=='l') {cout<<"01101100"<<endl;}
if(letter=='n') {cout<<"01101110"<<endl;}
if(letter=='o') {cout<<"01101111"<<endl;}
if(letter=='p') {cout<<"01110000"<<endl;}
if(letter=='q') {cout<<"01110001"<<endl;}
if(letter=='r') {cout<<"01110010"<<endl;}
if(letter=='s') {cout<<"01110011"<<endl;}
if(letter=='t') {cout<<"01110100"<<endl;}
if(letter=='u') {cout<<"01110101"<<endl;}
if(letter=='v') {cout<<"01110110"<<endl;}
if(letter=='w') {cout<<"01110111"<<endl;}
if(letter=='x') {cout<<"01111000"<<endl;}
if(letter=='y') {cout<<"01111001"<<endl;}
if(letter=='z') {cout<<"01111010"<<endl;}
getch();
}
return 666;}