簡體   English   中英

如何比較ASCII值

[英]How to compare an ASCII value

我想將字母的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文件? 所以它可以保存在字符變量上。

做就是了:

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

}

如果您只想將ascii值保存到整數變量中

只是用這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM