简体   繁体   English

Int 到 ASCII 字符和 char 到 ASCII 数字 | C++

[英]Int to ASCII char and char to ASCII number | C++

I must write short script which will change int to ASCII char and char to ASCII int.我必须编写简短的脚本,将 int 更改为 ASCII char 并将 char 更改为 ASCII int。 But i don't know how.但我不知道怎么做。 I wrote this short code but something is wrong.我写了这个简短的代码,但是出了点问题。 I'm programming for half-year.我编程了半年。 Can someone write it working?有人可以写它吗? It is my first time to use functions in c++第一次使用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;
}

Thank You very much Guys, I have done it with shorter and working code.非常感谢你们,我用更短且有效的代码完成了它。

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

output: output:

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

Originally I thought you were just looking to convert the number: You can simply add and substract '0', to char and from char:最初我以为您只是想转换数字:您可以简单地将 '0' 加减到 char 和 char 中:

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

If you just want to cast the type, read this如果您只想转换类型,请阅读此

May be you could start with the code below.也许你可以从下面的代码开始。 If this is incomplete, could you please complete your question with test cases?如果这不完整,您能否用测试用例完成您的问题?

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


The output is: output 是:

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

you could also use printf, so you don't need to create other function for converting the number.您也可以使用 printf,因此您不需要创建其他 function 来转换数字。

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

the output is gonna be output 将是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM