-2

I'm prompting for a user supplied string using fgets(user_input, input_len, stdin). How can I send, for instance, the byte represented by hex \x04 to the program?

  • It depends on your terminal (or other input system you're using). – sidyll Mar 23 '16 at 18:04
  • Are you having the problem that `0x04` is control-D is EOF? – Steve Summit Mar 23 '16 at 18:32
  • @SteveSummit 0x04 is described as EOT in the ascii table, this is a char used by various serial transmission protocols as end of transmission. This, however, has nothing to do with ctrl+d on the console which on many systems results in an EOF on the input stream. – Ctx Mar 23 '16 at 19:04
  • @Ctx Sure, but if I wanted to send the byte represented by hex \x04 to my program, I'd simply type control-D, and unless I was careful I'd get EOF instead, and that *might* be user6105782's problem. – Steve Summit Mar 23 '16 at 19:22

2 Answers2

2

You can do

$ echo -n -e '\x04' | your-program

NOTE: On POSIX echo only octal values are allowed.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
1

If you can get the bytes you want into a file, you can run

your-program < file
Steve Summit
  • 45,437
  • 7
  • 70
  • 103