标准输入在不同的编程语言中有奇怪的字符 [英] Standard Input having weird characters with them in different programming lanuage
问题描述
我对这些编程语言的标准输入感到困惑:
I am getting confused with the standard input of these programming languages :
我添加了有关这么多编程语言的详细信息,因为所有这些语言的问题在这件事上都是一样的,我在这个问题中唯一的重点是如何克服这个问题并拥有一个真正的终端,例如来自程序本身的体验.
I added details about so many programming languages as the problem with all of them is same in this matter and my only focus in this question is how can I overcome this problem and have a true terminal like experience from within the program itself.
首先
代码:
import java.util.Scanner;
public class Try{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter a String : ");
String s = sc.nextLine();
System.out.println("The Entered String is : " + s);
System.out.println("The Length of Entered String is : " + s.length());
sc.close();
}
}
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
当我按下方向键时 ^[[C
出现而不是光标移动(其他方向键、escape 键、home、end 也会发生类似的情况)!
When I press the arrow keys ^[[C
show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!
但即使在这种情况下,它是如何变成 8 的?当我打印它们时,为什么它们没有显示为 '['、'C'、'^' 是可打印的.
But even in this case how it is becoming 8? and when i print them why are they not showing up as '[', 'C', '^' are printable.
这里和 Java 一样!
Same as Java here!
代码:
s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python try.py
Enter a String :
hello^[[D
The Entered String is : hello
The Length of Entered String is : 8
C
这里也一样..
代码:
#include <stdio.h>
int main()
{
char s[20];
int len = 0;
printf("Enter a String : \n");
scanf("%[^\n]%*c", s);
while (s[len] != '\0')
len++;
printf("The Entered String is : %s\n", s);
printf("The Length of Entered String is : %d\n", len);
return 0;
}
输出:
─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
但代码略有不同:
代码:
#include <stdio.h>
#define MAX_LIMIT 20
int main()
{
char s[MAX_LIMIT];
int len = 0;
printf("Enter a String : \n");
fgets(s, MAX_LIMIT, stdin);
while (s[len] != '\0')
len++;
printf("The Entered String is : %s\n", s);
printf("The Length of Entered String is : %d\n", len);
return 0;
}
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 6
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 9
这里我们可以清楚地看到它也将 \n
作为字符串的一部分,所以 9
Here we cal see clearly that it takes the \n
too as a part of the string so 9
这里也一样..
代码:
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cout << "Enter a string : " << endl;
cin >> s;
cout << "The Entered String is : " << s << endl;
cout << "The Length of Entered String is : " << s.length() << endl;
return 0;
}
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $g++ -o trycpp -Os try.cpp
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./trycpp
Enter a string :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
重击
这里也一样..代码:
#!/bin/bash
echo "Enter a String : "
read str
echo "The Entered String is : $str"
len=`expr length "$str"`
echo "The Length of Entered String is : $len"
输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String :
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./try.sh
Enter a String :
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8
异常
但这一切都有一个例外
Exception
But to all this there is a exception
它是用蟒蛇.
如果我们直接使用 python 解释器,而不是从文件中运行代码:
Instead of running code from a file if we use the python interpreter directly:
这是终端输出:
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $python
Python 3.9.2 (default, Feb 28 2021, 17:03:44)
[GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> s = input("Enter a String : \n")
Enter a String :
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
>>> s = input("Enter a String : \n")
Enter a String :
hello
>>> print("The Entered String is : " + s)
The Entered String is : hello
>>> print("The Length of Entered String is : " + str(len(s)))
The Length of Entered String is : 5
在这里,尽管按箭头键或退出或主页,输出是相同的.
Here in spite of pressing the arrow keys or escape or home the output is same.
我查看了字符串的长度,使其长度为 8 个字符:
I looked in whats the string is having thats making it 8 characters long:
['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']
但这里也[
和C
是可打印的!
But here too [
and C
is printable !
谁能解释一下这一切是怎么回事?
Can any one explain whats all these about?
我怎样才能摆脱它们?
如果您需要更多细节或清晰度,请询问
If you need any more details or clarity please ask
推荐答案
怎么变成8了?当我打印它们时,为什么它们没有显示为 '['、'C'、'^' 是可打印的.
how it is becoming 8? and when i print them why are they not showing up as '[', 'C', '^' are printable.
您在按右箭头键时看到的三个 char
组合在一起形成一个转义序列.第一个字符是 ESC
.
The three char
s you see when pressing the right arrow key are combined to form an escape sequence. The first character being ESC
.
ESC
不可打印,但可能会被您的终端消耗,终端将进入等待更多内容的状态.当它到来时,它会采取行动.
ESC
is not printable but is likely consumed by your terminal which is going into a state where it's waiting for something more to come. When it comes, it'll act on it.
0x1b // ESC
0x5b // [ - CSI - Control Sequence Introducer
0x43 // C - CUF - Cursor Forward
如果您从输出中删除 ESC
,您的终端将很乐意打印 [C
但是当前面有 ESC
时,它会形成一个命令为如上所示.
If you remove the ESC
from the output, your terminal will gladly print [C
but when preceeded by ESC
it forms a command as shown above.
这篇关于标准输入在不同的编程语言中有奇怪的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!