从 C/C++ 中的数字获取 ASCII 字符

发布于 2024-12-28 08:07:03 字数 922 浏览 0 评论 0原文

我的要求是,当我输入一个数字(比如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

開玄 2025-01-04 08:07:03

您在那里定义自己的字符编码。

除以 10 并迭代取模 10 来得到数字,然后使用

char to_ascii (int i) {
    return 'A' + i - 1;
}

// ...

assert (to_ascii(0x01) == 'A');

char buffer [20] = {0};
for (...) {
    buffer [i] = to_ascii (n);
}

printf (buffer);

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

char to_ascii (int i) {
    return 'A' + i - 1;
}

// ...

assert (to_ascii(0x01) == 'A');

char buffer [20] = {0};
for (...) {
    buffer [i] = to_ascii (n);
}

printf (buffer);

How on earth did you get 90='Z'?

誰認得朕 2025-01-04 08:07:03

您收到的运行时错误是因为 %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.

枯叶蝶 2025-01-04 08:07:03

您好,您可以尝试这个简单的方法:


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

return 0;
}

Hi, you can try this simple method :


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

return 0;
}
稳稳的幸福 2025-01-04 08:07:03
#include <stdio.h>
#include <string.h>

char* numToColumn(int n, char* outstr){
    char* p = outstr;
    while(n){
        *p++ = 'A' + ((n % 26 == 0)? 26 : n % 26) - 1;
        n = (n - 1) / 26;
    }
    *p = '\0';
    return strrev(outstr);//strrev is win32 not ANSI C
}

int main(){
    char buff[16];

    printf("%s\n", numToColumn( 1,buff));//A
    printf("%s\n", numToColumn( 5,buff));//E
    printf("%s\n", numToColumn(26,buff));//Z
    printf("%s\n", numToColumn(27,buff));//AA
    printf("%s\n", numToColumn(52,buff));//AZ
    printf("%s\n", numToColumn(53,buff));//BA
    return 0;
}
#include <stdio.h>
#include <string.h>

char* numToColumn(int n, char* outstr){
    char* p = outstr;
    while(n){
        *p++ = 'A' + ((n % 26 == 0)? 26 : n % 26) - 1;
        n = (n - 1) / 26;
    }
    *p = '\0';
    return strrev(outstr);//strrev is win32 not ANSI C
}

int main(){
    char buff[16];

    printf("%s\n", numToColumn( 1,buff));//A
    printf("%s\n", numToColumn( 5,buff));//E
    printf("%s\n", numToColumn(26,buff));//Z
    printf("%s\n", numToColumn(27,buff));//AA
    printf("%s\n", numToColumn(52,buff));//AZ
    printf("%s\n", numToColumn(53,buff));//BA
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文