2011-12-28 39 views
0

爲什麼CTRL + M給出了一個10位的ASCII值(十進制值)。它實際上應該是13.我通過putty連接到Amazon EC2 linux實例。我執行以下程序ascii Linux in Linux代碼

import java.io.IOException; 
import java.io.InputStream; 
import java.io.ByteArrayOutputStream; 

public class NumbersConsole { 

private static String ttyConfig; 

public static void main(String[] args) { 

     try { 
       setTerminalToCBreak(); 

       int i=0; 
       while (true) { 

         //System.out.println(""+ i++); 

         if (System.in.available() != 0) { 
           int c = System.in.read(); 
        System.out.println(c); 
           if (c == 13) { 
             break; 
           } 
         } 

       } // end while 
     } 
     catch (IOException e) { 
       System.err.println("IOException"); 
     } 
     catch (InterruptedException e) { 
       System.err.println("InterruptedException"); 
     } 
     finally { 
      try { 
       stty(ttyConfig.trim()); 
      } 
      catch (Exception e) { 
       System.err.println("Exception restoring tty config"); 
      } 
     } 

} 

private static void setTerminalToCBreak() throws IOException, InterruptedException { 

    ttyConfig = stty("-g"); 

    // set the console to be character-buffered instead of line-buffered 
    stty("-icanon min 1"); 

    // disable character echoing 
    stty("-echo"); 
} 

/** 
* Execute the stty command with the specified arguments 
* against the current active terminal. 
*/ 
private static String stty(final String args) 
       throws IOException, InterruptedException { 
    String cmd = "stty " + args + " < /dev/tty"; 

    return exec(new String[] { 
       "sh", 
       "-c", 
       cmd 
      }); 
} 

/** 
* Execute the specified command and return the output 
* (both stdout and stderr). 
*/ 
private static String exec(final String[] cmd) 
       throws IOException, InterruptedException { 
    ByteArrayOutputStream bout = new ByteArrayOutputStream(); 

    Process p = Runtime.getRuntime().exec(cmd); 
    int c; 
    InputStream in = p.getInputStream(); 

    while ((c = in.read()) != -1) { 
     bout.write(c); 
    } 

    in = p.getErrorStream(); 

    while ((c = in.read()) != -1) { 
     bout.write(c); 
    } 

    p.waitFor(); 

    String result = new String(bout.toByteArray()); 
    return result; 
} 

} 

,當我給輸入爲(CTRL +中號),我收到顯示值10,但我期待的13的值,請讓我知道我是否缺少什麼?

回答

3

CR到LF的轉換是通過處理tty驅動程序。你打電話setTerminalToCBreak(),它操縱tty設置(我認爲它禁用了擦除,殺死,werase和rprnt特殊字符)。

默認情況下啓用的icrnl設置會導致回車符(CR)被轉換爲換行符(LF)。禁用它應該讓你直接看到CR字符。設置raw模式會更改一些標誌,包括關閉icrnl。 (找出在Java中如何做到這一點是作爲一個練習。)

但要小心這樣做。 輸入返回鍵通常發送一個CR字符。將它翻譯爲LF可以讓它標記一行的結尾。如果您關閉該翻譯,除非您自己處理CR,否則可能會中斷該行爲。

有關tty設置的更多信息,請參閱man tty或關注this link

+0

正確。有問題的設置是每個用戶設置(與您的tty登錄關聯),而不是「Linux」本身。從命令提示符處輸入「man tty」以瞭解更多信息。或者輸入「stty sane」;) – paulsm4 2011-12-28 23:07:35

+0

PS:在DOS/Windows上,點擊會生成* two *個字節:回車符(13)和換行符(10)。同樣,寫入「\ n」的C,C#或Java程序在DOS/Windows上生成相同的兩個字節。 – paulsm4 2011-12-28 23:11:55

+0

@ paulsm4在Windows上將\ n轉換爲\ r \ n只能由C規範指定,並且不能在Java中完成(即,在Java中編寫\ n,標準類實際上只應寫入一個字符)。不知道C#。 – Voo 2011-12-29 00:31:01

0

我的其他答案開始在完全錯誤的頁面上。

stty ("-cooked") 

適合我。

電傳式土地深處的東西希望你有快樂的小^J s而不是^M s,但烹調終端停止它。

 $ stty -cooked ; java -cp /tmp NumbersConsole 
     13 

     $ 

回到好日子,一些電腦(Commodore,Apple)用^ M(13)作爲返回鍵;一些(IBM)使用組合^ M^J;其他人(Unix)使用^ J(10)。現在

,在現代世界中,它幾乎總是道^ J(雖然我認爲Windows代碼仍然有某些引擎蓋下的遺留^ M道^ J東西有時?)

+0

DOS/Windows仍然是「回車/換行」:#13#10 – paulsm4 2011-12-28 23:09:02

+0

@BRPocock謝謝!! – karanece 2011-12-28 23:15:14

+0

我不知道這是否是對菊花輪印刷時代的迷人倒退,還是僅僅是一個令人不安的提醒......但是,我忘記了我們在堆棧的應用程序級別有多麼被寵壞,我完全忘記了HTTP也使用^ M^J。 – BRFennPocock 2011-12-28 23:15:40