In my usual gnome-terminal I do a

netcat -u somewhere 1234

to start sending UDP packets to somewhere. I need to transmit the following 5 bytes, written in hexadecimal: "01 00 af 0f e1". Now how do I type a escape sequence into my terminal that causes it to send these exact 5 bytes to stdin of netcat?

Update: Just to clarify. I know several ways of actually inputting the bytes I want to the program. That is not the question. The question is what sequence of keystrokes do I type into my terminal, after the program is launched, to input a hexadecimal character that is not otherwise represented on my keyboard. (Such as 0x00, 0x01, or 0x0f.)

I am starting to think it is simply not possible, however I would be a bit surprised if that is the conclusion.


You can use echo -e, (which is what I think richardhoskins meant.) Like this:

/bin/echo -n -e "\x01\x00\xaf\x0f\xe1" | netcat -u somewhere 1234
# -n: no newline at end, -e: interpret escapes

If your echo implementation is old, (or simply not GNU-encrusted,) the old-school way is to use octal:

/bin/echo -n -e "\001\000\257\017\341" | netcat -u somewhere 1234

Yes, octal is way, way old-school. Look at ibase and obase in the man for bc. Check to see if echo does the right thing with od ("octal dump") "od -tx1" for hex, "od -to1" for octal.


A simple approach, though not quite what you are looking for...

Use a hex editor to create a file with the bytes in that you want to send (you mention gnome so you may already have ghex but any hex editor should be fine). For short hex sequences I usually include the hex as part of the name (e.g. test0x0100af0fe1).

Then just redirect the content into netcat, e.g.:

mas@voco:~$ nc -o testout 80 < test0x0100af0fe1
mas@voco:~$ cat testout
> 00000000 01 00 af 0e 10                                  # .....

This has the disadvantage of having to prepare a separate file but it makes replication and documentation of tests easier.

  • I will probably end up using a solution like this, but I really thought there was a way to escape input in the terminal so that I may type the hexadecimal representation of a character and the terminal will translate it before sending it as input to the program. I like the idea of creating files with the content in the filename, it is exactly very short strings I have to transmit, thanks. Aug 7 '09 at 7:11

you have to use perl in terminal example:

./program perl -e' print "\x41\x60\x34\x45"'

it will send this hex address to program. It is used often in buffer overrun as hexa address input. The page does not show everything I typed. Look on google for buffer overflow. Those pages show you everything. Here are missing back single quotes in front of perl one and second at the end of a whole input.

  • Nice, Hex to ASCII sender.
    – YumYumYum
    May 9 '12 at 10:49

Append \x to the beginning of the byte. e.g. "\x01 \x00 \xaf \x0f \xe1".

  • 1
    Nope, just tried it, it does not escape anything in the terminal. Typing "0xaf" becomes "30 78 61 66 0a" as input to the program. Aug 6 '09 at 15:03
  • What shell are you using? Aug 6 '09 at 15:07
  • I am using bash, but I would think only the terminal mattered. The input is going directly to the program (netcat). Aug 6 '09 at 15:10
  • Edited to \xHH from 0xHH. Aug 6 '09 at 15:37
  • 1
    Thanks, but again, the input is just "5c 78 61 66 0a". Neither 0x nor \x escapes anything in the terminal. :/ Aug 7 '09 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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