Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I need to be able to paste binary data into a terminal. For some reason, every byte outside the ASCII range (0x80-0xff) is pasted as the same three byte sequence 0xef 0xbf 0xbd.

For example:

$ echo -en "\x80" | xclip
$ hd
<paste><EOF>
00000000  ef bf bd                                       |...|
00000004

It has something to do with the character encoding used by the terminal since if I change it from UTF-8 to ISO 8859 or similar every character in the extended range is translated to 0x3f.

Does anybody have an idea on how to paste arbitrary binary data into the terminal?

Edit: This seems to be very terminal dependent. The example above is in Konsole. I get the desired behavior in xterm and Gnome Terminal doesn't allow to paste characters in the extended range at all. Any Konsole specific solution would still be appreciated.

share|improve this question

3 Answers 3

ef bf bd is the UTF-8 encoding of REPLACEMENT CHARACTER (�), which is "used to replace an incoming character whose value is unknown or unrepresentable in Unicode".

What you describe is not "extended ASCII", but rather binary data. Some bytes in the range 0x80-0xff are not valid ISO 8859 anything, so it's understandable that some programs treat that as an unknown character.

You could try using an 8-bit character encoding that uses all 255 positions, such as IBM code page 850.

But then the program you're copying from might be interpreting the data too. And what happens when you paste a null byte or a terminal escape sequence? The whole approach seems destined to fail.

share|improve this answer
    
That sounds reasonable but why then does it work in xterm? –  Job Jun 25 '12 at 17:48
    
I guess it's passing thru unrecognized data rather than trying to interpret it. –  Mikel Jun 25 '12 at 21:06
    
IBM 850 also seems to translate those "high" bytes to 0x3f. –  Job Jun 26 '12 at 6:34

Terminals are generally not designed to accept binary input: they expect control characters to have a special meaning in applications, and do some processing of control characters themselves (mostly into a few signals).

An exception is Emacs's term mode (or one of its variants), which treats pasted data as raw text that's passed on to the application.

The normal method of providing binary input to an application would be to redirect its input from a file or pipe. If the data is in the X clipboard, you can use xclip or xsel:

xclip -o | myapp
xsel -o | myapp
share|improve this answer
    
The problem is that after having provided the binary data, the program accepts normal user input again. This can be tackled by using named pipes, something like cat pipe - | myapp. But since this is meant to be a demonstration for people who do not necessarily have a programming background, I wanted to keep it as simple as possible and just find a way to paste binary data in the terminal. –  Job Jun 26 '12 at 6:32

Well, I would like just to post a comment in the question, but I still have not enough reputation.

The expected behaviour worked here using yakuake terminal. I've done echo -en "\x5" | xclip and then middle button clicked on a screen session with a serial port opened on it. The device echoed just as expected.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.