爲什麼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的值,請讓我知道我是否缺少什麼?
正確。有問題的設置是每個用戶設置(與您的tty登錄關聯),而不是「Linux」本身。從命令提示符處輸入「man tty」以瞭解更多信息。或者輸入「stty sane」;) – paulsm4 2011-12-28 23:07:35
PS:在DOS/Windows上,點擊會生成* two *個字節:回車符(13)和換行符(10)。同樣,寫入「\ n」的C,C#或Java程序在DOS/Windows上生成相同的兩個字節。 –
paulsm4
2011-12-28 23:11:55
@ paulsm4在Windows上將\ n轉換爲\ r \ n只能由C規範指定,並且不能在Java中完成(即,在Java中編寫\ n,標準類實際上只應寫入一個字符)。不知道C#。 – Voo 2011-12-29 00:31:01