Standard Input having weird characters with them in different programming lanuage

2024/7/8 7:04:28

I am getting confused with the standard input of these programming languages :

[Note:]

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.

Firstly,

Java

Code:

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();}
}

Output:

┌─[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

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)!

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.

Python

Same as Java here!

Code:

s = input("Enter a String : \n")
print("The Entered String is : " + s)
print("The Length of Entered String is : " + str(len(s)))

Output:

┌─[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

Same thing here too..

Code :

#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;
}

Output:

─[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

But with a slightly different code :

Code:

#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;
}

Output:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $gcc -o tryc -Os try.c
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello
The Entered String is : helloThe Length of Entered String is : 6
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $./tryc
Enter a String : 
hello^[[C
The Entered String is : helloThe Length of Entered String is : 9

Here we cal see clearly that it takes the \n too as a part of the string so 9

C++

Same here too ..

Code:

#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;
}

Output:

┌─[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

Bash

Same here too.. Code:

#!/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"  

Output:

┌─[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

It is with python.

Instead of running code from a file if we use the python interpreter directly:

Here is the Terminal output :

┌─[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.

I looked in whats the string is having thats making it 8 characters long:

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

But here too [ and C is printable !

Can any one explain whats all these about?

And how can i get rid of them?

If you need any more details or clarity please ask

Answer

how it is becoming 8? and when i print them why are they not showing up as '[', 'C', '^' are printable.

The three chars you see when pressing the right arrow key are combined to form an escape sequence. The first character being 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

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.

https://en.xdnf.cn/q/119525.html

Related Q&A

How to turn a numpy array to a numpy object?

I have a NumPy array as follows: [[[ 0 0]][[ 0 479]][[639 479]][[639 0]]]and I would like to convert it into something like so: [( 0 0)( 0 479)(639 479)(639 0), dtype=dtype([(x, <i2), (y…

recover all line from an attribute in a database in json

To simplify my problem, I have a base in json, and I recover all of my lines of json to put informations in a base. It seems easy for moments, but problem is that my json is not correctly writtenSo i…

Calculate eigen value in python as same way(order) in Matlab

This is the Matlab code which is returning eigenvector in V and eigenvalue in D. Consider C is 9*9 matrix then V is 9*9 matrix and D is 9*9 diagonal. matrix.[V,D] = eig(C);I want the same thing in Pyth…

Python: ctypes and Pointer to Structure

I am trying to make a pointer of a struct and then de-reference it. But its crashing. I have mimiced the behvior here with this simple code. from ctypes import * import ctypesclass File(Structure):_fie…

Python:Why readline() function doesnt work for file looping

I have the following code:#!/usr/bin/pythonf = open(file,r)for line in f:print line print Next line , f.readline() f.close()This gives the following output:This is the first lineNext line That was the …

How to replace cropped rectangle in opencv?

I have managed to cropped a bounding box with text, e.g. given this image:Im able to exact the following box:with this code: import re import shutilfrom IPython.display import Imageimport requests impo…

How can I read hexadecimal data with python?

I have this c# app that Im trying to cooperate with a app written in python. The c# app send simple commands to the python app, for instance my c# app is sending the following:[Flags]public enum GameRo…

Want to scrape all the specific href from the a tag

I have search the specific brand Samsung , for this number of products are search ,I just wanted to scrape all the href from the of the search products with the product name . enter code here import u…

Encryption code in def function to be written in python

need some help in the following code as it goes into infinite loop and does not validate user input: the get_offset is the function. Just edited need some help with the encryption part to be done in a …

Creating xml from MySQL query with Python and lxml

I am trying to use Python and LXML to create an XML file from a Mysql query result. Here is the format I want.<DATA><ROW><FIELD1>content</FIELD1><FIELD2>content</FIELD2…