从 C/C++ 中的数字获取 ASCII 字符
我的要求是,当我输入一个数字(比如 01 - 100)时,我应该能够获得该数字的 ASCII 代码值。
例如:01 = A,02 = B,03 = C,依此类推......,90 = Z,如果数字是 91 = AA,92 = AB,93 = AC 等......
我可以从谷歌搜索等...是将 int 转换为 char,获取 ASCII 值,但这仅在使用“%c”打印时发生,但无法保存到 CHAR 或 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 时,它无法打印。
真的很抱歉没有正确发布我的问题,可能是我无法正确提出问题,因为这是我的第一篇文章...
我正在编辑我的帖子,这可能不太清楚...
我输入的输入将是 1 (或任何整数,比如 1 - 100),我应该能够得到该数字的十进制 ASCII 表示...
假设 1 是 65,目前我正在做的是,我首先减去 1,然后加上 65它...
例如:如果输入是5,它将是 5 - 1 + 65 =
69,它代表大写字母 E
同样的方式,如果输入是 15,我的输出应该是大写 O
希望这有点清楚......
my requirement is, when I input a number (say 01 - 100), I should be able to get the ASCII code value for that number.
Ex: 01 = A, 02 = B, 03 = C, so on...., 90 = Z, and if number is 91 = AA, 92 = AB, 93 = AC, etc...
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.
Ex:
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.
While printing it is printing the values properly but while saving it to String or Char it not able to.
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...
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...
assuming 1 is 65, currently what I am doing is, I am first subtracting 1 and then add 65 to it...
for ex: if the input is 5, it would be 5 - 1 + 65 =
69, which represents uppercase letter E
same way if the input is 15, my output should be Uppercase O
hope this is little clear...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在那里定义自己的字符编码。
除以 10 并迭代取模 10 来得到数字,然后使用
How on Earth did you get
90='Z'
?You're defining your own character encoding there.
Divide by ten and take modulo ten iteratively over to get the digits, then use
How on earth did you get
90='Z'
?您收到的运行时错误是因为
%s
需要一个指向以 null 结尾的字符数组的指针。您正在传递一个字符。这样做会产生未定义的行为。幸运的是,您遇到了运行时错误,这使得该错误变得显而易见。目前尚不清楚您要在这里做什么,因此不知道最好的解决方案是什么。
The runtime error you get is because
%s
requires a pointer to a null-terminated array of characters. You're passing a character instead. Doing this produces undefined behaviour. Luckily you're getting a runtime error, which makes the bug obvious.It is not clear what you are trying to do here so have no idea what the best solution for this would be.
您好,您可以尝试这个简单的方法:
Hi, you can try this simple method :