如何比较 ASCII 值

发布于 2024-12-05 11:59:15 字数 386 浏览 6 评论 0原文

我想将字母的 ASCII 值存储到变量中,我该怎么做?

例如:

r ASCII variable = 82
main()
{
    character = "character read from a file";
    variable= "r ascii"; //(in this case 82), the problem is that the letter is always        variable.;
    printf( "the value of %c is %d, character, variable)
}

我该怎么做?

另请注意,如何逐个字符读取 .txt 文件?所以它可以保存在字符变量上。

I want to store the ASCII value of a letter into a variable, how can I do this?

for example :

r ASCII variable = 82
main()
{
    character = "character read from a file";
    variable= "r ascii"; //(in this case 82), the problem is that the letter is always        variable.;
    printf( "the value of %c is %d, character, variable)
}

How can I do this?

Also on an extra note, how could I read a .txt file character by character? so It could be saved on the character variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉殇 2024-12-12 11:59:15

只需执行以下操作:

if (r == 82) {
   // provided r is a char or int variable
}

C 中,char 变量由其 ASCII 整数值表示,因此,如果您有以下内容:

char r;
r = 82;
if (r == 82) {
}

相同:

char r;
r = 'R';
if (r == 'R') { // 'R' value is 82

} 

与以下内容 甚至可以混合它们:

char r;
r = 82;
if (r == 'R') { // will be true

}

Just do:

if (r == 82) {
   // provided r is a char or int variable
}

In C, char variables are represented by their ASCII integer value, so, if you have this:

char r;
r = 82;
if (r == 82) {
}

Is the same as:

char r;
r = 'R';
if (r == 'R') { // 'R' value is 82

} 

You can even mix them:

char r;
r = 82;
if (r == 'R') { // will be true

}
囍孤女 2024-12-12 11:59:15

如果您只想将 ascii 值保存到整数变量中,

只需使用它

int b;
char c = 'r';
b = (int)c;
printf("%d",b);

If you just want to save the ascii value onto an integer variable

just use this

int b;
char c = 'r';
b = (int)c;
printf("%d",b);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文