【问题标题】:Converting chars to binary in C++ [duplicate]在 C++ 中将字符转换为二进制 [重复]
【发布时间】:2015-12-08 22:12:52
【问题描述】:

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

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

}

【问题讨论】:

  • 了解如何在编译器/IDE 中打开警告。
  • @infiltrait0rN77,如果你有这么多if 语句,最好使用switch 并检查案例。喜欢switch (letter) {case 'A': cout&lt;&lt;"01000001"&lt;&lt;endl; break; ... }

标签: c++ loops binary chars


【解决方案1】:

当你编码时 -

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

当你编码时 -

如果(a=anything){statements},那么它将值(anything)赋给变量(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;}

【讨论】:

  • 您的解释不完整,实际上并没有解释简单的错字。此外,代码的格式也很奇怪。
  • 不要在 cmets 中放细节! Edit 答案并在那里进行更改。答案应该是独立的,并且可以删除 cmets。我没有要求您在评论中向我解释它,而是以清晰明了的方式为每个人提供您的答案。 (并修复损坏的格式。)请参阅How to Answer。 (顺便说一句,你的解释还是不太对。你知道为什么f=anything返回true吗?)
  • 好的,谢谢?建议
【解决方案2】:

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

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

无需硬编码转换。


另外请注意,如果这个问题是关于学术原因的,并且您被要求开发一个使用通用算法生成字符串的解决方案,您的解决方案将获得 F 我的学校笔记。

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

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

【讨论】:

  • 不,只是我在胡闹。感谢您的答复。 :)
  • @Infiltrait0rN77 用更多参考更新了我的答案。
【解决方案3】:

你想要if (letter == ...),而不是if (letter = ...)

“你能告诉我这里有什么问题吗?”

当你这样做时:

if (foo = bar)

...您将值 bar 影响到 foo。如果生成的 bar 值不为零,则条件为真。

欢迎编程。

【讨论】:

  • Legion 感谢您对他的帮助。 :D 我怎么错过了...再次感谢。
  • 当你对一个常量进行测试时,一个方便的习惯用法是if (constant == variable):如果你输入错误的'=',它将不会编译。 Sh*t 发生了,当复制/粘贴到所有地方时更糟。
猜你喜欢
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 2011-12-13
  • 2012-08-16
  • 2013-03-14
  • 2013-01-16
  • 2018-06-05
  • 2012-01-02
相关资源
最近更新 更多