Reputation: 4191
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
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:
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