Ari
Ari

Reputation: 4191

How to Manipulate Control Characters?

I'd appreciate suggestions on how I can convert ASCII control characters, which are supplied via an HTML text box, to their hexadecimal or binary representations.

Currently, my web app, takes the ASCII control character string and converts the values, for example, if ^C is entered the value 5e43 is returned which represents "^" and "6", not control-c which is represented as 02 in hex.

The idea I had was to run a regex against the input to check for control characters with something like: ^[\w]{1} and then return values from a predefined table that matches the regex.

Upvotes: 2

Views: 393

Answers (1)

Arthur Ulfeldt
Arthur Ulfeldt

Reputation: 91554

You can directly read from in with (. *in* read) though how the characters get to you is going to be dependent on a lot of things, most specifically the case that the browser is likely to encode them for http transport before you even get started.

I maintain a secure terminal proxy that has to handle all combinations of control characters so I thought I would pass on a few notes:

  • they are not one character long. you need up to six characters to represent them. try hitting Esc-Ctrl-alt-left-arrow.
  • esc implies Alt but alt does not imply esc. if the first character is an esc then the next character is the meta/alt of its character value. so if you see esc-b this is Alt-b
  • some keys (page up for example) send the esc automatically.
  • esc-esc is it's own thing (which I can't say I fully understand).

The best way is to write a small program that reads from the keyboard one character at a time and then start mashing the keyboard and see what you can come up with.

Here I will read a character from in twice and hit home the first time and end the second

clojure.core=> (. *in* read)
10
clojure.core=> (. *in* read)
10

So clearly one character is not enough to distinguish these two keys, how about two characters?

This next example won't run in the repl because the repl tries to "handle" control character for you, so you will have to make a new project lein new esc add this code then lein uberjar and java -jar esc-1.0.0-SNAPSHOT-standalone.jar

(ns esc.core
  (:gen-class))

(defn -main []
  (dorun (repeatedly #(println (. *in* read)))))

Running it and hitting these two keys produces this:

^[OF
27
79
70
10      <-- this is the newline
^[OH
27      <-- esc start marker look for this
79
72
10      <-- this is the newline

Here is esc-end

^[^[OF
27
27
79
70
10

And the ctrl character grand prize winner thus far esc-right-arrow

^[[1;5C
27
91
49
59
53
67
10

taking the prize at six bytes.

Upvotes: 5

Related Questions