Int 到 ASCII 字符和 char 到 ASCII 数字 | C++
Int to ASCII char and char to ASCII number | C++
我必须编写简短的脚本,将 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;
}
#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
也许您可以从下面的代码开始。如果这不完整,你能用测试用例完成你的问题吗?
#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
你也可以使用 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
非常感谢你们,我已经用更短的工作代码完成了。
#include <iostream>
using namespace std;
int main(){
int a=68;
cout<<char(a);
char c='D';
cout<<int(c);
return 0;
}
我必须编写简短的脚本,将 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;
}
#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
也许您可以从下面的代码开始。如果这不完整,你能用测试用例完成你的问题吗?
#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
你也可以使用 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
非常感谢你们,我已经用更短的工作代码完成了。
#include <iostream>
using namespace std;
int main(){
int a=68;
cout<<char(a);
char c='D';
cout<<int(c);
return 0;
}