从C / C ++中的数字获取ASCII字符 [英] Get ASCII character from number in C/C++

查看:1046
本文介绍了从C / C ++中的数字获取ASCII字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是,当我输入一个数字(说01 - 100),我应该能够得到该数字的ASCII码值。

my requirement is, when I input a number (say 01 - 100), I should be able to get the ASCII code value for that number.

01 = A,02 = B,03 = C,so on ....,90 = Z,如果number是91 = AA,92 = AB,93 = AC,etc ...

Ex: 01 = A, 02 = B, 03 = C, so on...., 90 = Z, and if number is 91 = AA, 92 = AB, 93 = AC, etc...

所有我可以从googling等等得到的是将int转换为char,获取ASCII值,但这仅发生在使用%c打印但不能保存到CHAR或STRING。

All I could get from googling, etc...was to convert int to a char, get ASCII value, but that happens only while printing using "%c" but not able to save into a CHAR or STRING.

例如:

int inputNumber=5;  
char getASCIICharValue = (char)inputNumber;  
printf("\n getASCIICharValue: %c  \n", getASCIINumberValue);  
// Above would print 'E' which is correct  
printf("\n getASCIICharValue as char: %s  \n", getASCIINumberValue);  
// Above results in a RUN-TIME error.  

打印时正在正确打印值,但保存为String或Char。

While printing it is printing the values properly but while saving it to String or Char it not able to.

真的很抱歉没有正确发布我的问题,可能是我无法正确地提出问题,因为这是我的第一post ...

really sorry for not posting my issue correctly, may be I couldn't put the question properly as this is my first post...

我正在编辑我的帖子,可能不太清楚...

I am editing my post which may make it little clear...

将输入将是1(或任何整数,例如1 - 100),我应该能够得到该数字的十进制的ASCII表示...

input I would enter would be 1 (or any integer, say 1 - 100), and I should be able to get the ASCII representation of that number in decimal...

1是65,目前我正在做的是,我第一个减去1,然后添加65到它...

assuming 1 is 65, currently what I am doing is, I am first subtracting 1 and then add 65 to it...

例如:如果输入为5,将是5 - 1 + 65 =

for ex: if the input is 5, it would be 5 - 1 + 65 =

69,它代表大写字母E

69, which represents uppercase letter E

是15,我的输出应该是大写O

same way if the input is 15, my output should be Uppercase O

希望这很清楚...

推荐答案

您好,您可以尝试以下简单方法:

int main()
{
int i;
cin>>i;
cout<< (char)i <<"\n"; 

return 0;
}

这篇关于从C / C ++中的数字获取ASCII字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆