【问题标题】:Int to ASCII char and char to ASCII number | C++Int 到 ASCII 字符和 char 到 ASCII 数字 | C++
【发布时间】:2021-02-26 14:14:24
【问题描述】:

我必须编写一个简短的脚本,它将 int 更改为 ASCII char 并将 char 更改为 ASCII int。但我不知道怎么做。我写了这个简短的代码,但是出了点问题。我正在编程半年。 有人可以编写它吗?这是我第一次在c++中使用函数

#include <iostream>
#include <conio.h>

using namespace std;
char toChar(int n) {
return n + '0';
}
int toInt(char c) {
return c - '0';
}
int main()
{
int number;
cout << "Int: ";
cin >> number;
cout << "ASCII: " << static_cast<char>(number);
getch();
return 0;
}

【问题讨论】:

  • 到底出了什么问题?当前输出和预期输出是多少?你是如何运行程序的(你输入的数字是多少)?另请参阅minimal reproducible example
  • 假设您有int x = 1;,您可以将其解释为ascii 1,或者您可以获得字符'1' 的ascii 代码。为了确定你想要什么,你应该包括输入、输出和预期输出
  • 如果_getch 有错误,请不要使用_getch!请改用getch
  • @Dock 好的,我会试试这个
  • 我编辑了它,但我不知道如何使用这个函数 toChar 和 toInt

标签: c++ ascii


【解决方案1】:

非常感谢你们,我已经用更短且有效的代码完成了。

#include <iostream>
using namespace std;
int main(){
int a=68;
cout<<char(a);
char c='D';
cout<<int(c);
return 0;
}

【讨论】:

    【解决方案2】:

    你也可以使用printf,所以你不需要创建其他函数来转换数字。

    int i = 64;
    char c = 'a';
    
    printf("number to ascii corresponding to %d is %c\n", i, i);
    printf("ascii to number corresponding to %c is %d\n", c, c);
    

    输出会是

    number to ascii corresponding to 64 is A
    ascii to number corresponding to a is 97
    

    【讨论】:

      【解决方案3】:

      也许你可以从下面的代码开始。如果这不完整,您能否用测试用例完成您的问题?

      #include <iostream>
      
      
      using namespace std;
      
      char toChar(int n)
      {   //you should test here the number shall be ranging from 0 to 127
          return (char)n;
      }
      
      int toInt(char c)
      {
          return (int)c;
      }
      
      int main()
      {
          int number = 97;
          cout << "number to ascii corresponding to  " << number << " is " <<(char)number << " or " << toChar(number) <<endl;
      
          char car='H';
          cout << "ascii to number corresponding to " << car << " is " << (int)car << " or " << toInt(car) << endl;
      
          return 0;
      }
      
      
      

      输出是:

      number to ascii corresponding to  97 is a or a
      ascii to number corresponding to H is 72 or 72
      
      

      【讨论】:

        【解决方案4】:
        #include <iostream>
        
        using namespace std;
        
        char toChar(int n)
        {
            if (n > 127 || n < 0)
                return 0;
            return (char)n;
        }
        
        int toInt(char c)
        {
            return (int)c;
        }
        
        int main()
        {
            int number = 97;
            cout << "char corresponding to number " << number << " is '" <<  toChar(number) << "'\n";
        
            char car='H';
            cout << "number corresponding to char '" << car << "' is " << toInt(car) << "\n";
        
            return 0;
        }
        

        输出:

        char corresponding to number 97 is 'a'
        number corresponding to char 'H' is 72'
        

        最初我以为您只是想转换数字: 您可以简单地在 char 和 char 中添加和减去“0”:

        char toChar(int n) {
            return n + '0';
        }
        
        int toInt(char c) {
            return c - '0';
        }
        

        如果你只是想cast the type, read this

        【讨论】:

        • 我应该在哪里添加这个?
        • 主函数上方
        • @KacperPodsiadło • 你为什么把_getch放在代码中?
        • @KacperPodsiadło 如果您在添加这两个函数后收到_getch 的错误,它应该已经存在了。您应该在问题中解释出了什么问题并包含错误消息。
        • @AntoninGAVREL 我还是不知道该怎么办,你能写出来但工作还是再帮忙一次?
        猜你喜欢
        • 1970-01-01
        • 2017-03-08
        • 1970-01-01
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 2023-03-31
        • 2016-10-13
        • 1970-01-01
        相关资源
        最近更新 更多