2014-02-22 31 views
0

我有一個問題我想退出while語句時,按德空格鍵 但在C無法識別十六進制數字,下面的代碼讀取功能。在C讀取功能無法識別十六進制

while (c!='0x20') 
     { 
       if (read(tty_fd,&c,1)>0)  write(STDOUT_FILENO,&c,1); 
// if new data is available on the serial port, print it out 
       if (read(STDIN_FILENO,&c,1)>0) 
       { 
        printf("\ningresaste %c ",c); 
        write(tty_fd,&c,1); 
// if new data is available on the console, send it to the serial port 
       } 
     } 

對不起我的英文。

+1

!C = 0×20,應該是這樣的 – 999k

+0

0x20的是十進制32或者你可以只使用'「」' – keshlam

+0

是「」,這沒關係。 – Premier

回答

4

這是錯誤的:

while (c!='0x20') 

它指定文本基礎上引號內的文字,而不是他們的數值多字符。

您應該使用

while (c!= ' ') // Because 0x20 is a space 

while (c!= 0x20) // Hexadecimal integer literal 

while (c!='\x20') // Character literal with hexadecimal value 
+0

非常容易的人,謝謝 – Premier

+0

+1三種不同的方式來指定一個字符的很好的解釋。蛋糕上的櫻桃將解釋原來的錯誤究竟是什麼...... – Floris

+1

@ user3338446請注意,即使有很多替代方法顯示,這種情況下的最佳方式肯定是'c!=''',以便讀者不會'不必知道'0x20'是一個空格。 – Arkku