gyuunyuu
gyuunyuu

Reputation: 684

How to insert ASCII control characters into a TCL string?

I need to create a TCL script that contains ASCII control characters. This is the full list of these characters from the ASCII table but I am only interested in putting in the "start of text" value 2 and "end of text" value 3.

enter image description here

Upvotes: 0

Views: 1726

Answers (3)

Donal Fellows
Donal Fellows

Reputation: 137577

If I'm going to be wrapping text in a control sequence (often for things like applying a colouring) then I'll make a procedure to do the job:

proc wrapped {string} {
    # These use Unicode escapes
    return "\u0002$string\u0003"
}

puts [wrapped "this is some test text"]

Upvotes: 1

Shawn
Shawn

Reputation: 52374

You can also use format with the %c code (which might be more useful if you don't know the relevant number until run-time because it's in a variable or whatever):

set ascii(STX) [format %c 2]
set ascii(ETX) [format %c 3]

Upvotes: 2

Colin Macleod
Colin Macleod

Reputation: 4382

You can enter a hex code in a string by writing \xnn where nn is the code, e.g.

set start_of_text "\x02"
set end_of_text "\x03"

See the documentation at https://www.tcl-lang.org/man/tcl8.6/TclCmd/Tcl.htm#M27

Upvotes: 3

Related Questions